Opening .csv files sequentially using VBA (1 Viewer)

S(0RP!0N

New member
Local time
Today, 15:34
Joined
Nov 1, 2011
Messages
2
Hi Guys,

I have been modifying a program but somehow I'm stuck.

Here's the program:

Code:
   Dim g As Integer
    g = 0
    Do
    g = g + 1
        Select Case g
        Case 1

        Workbooks.Open Filename:="C:\File directory\Log\P" & i & ".csv"
        Case 2
        Workbooks.Open Filename:="C:\File directory\Log\N" & i & ".csv"
        Case 3
            Workbooks.Open Filename:="C:\File directory\Log\L" & i & ".csv"
        End Select
    Loop Until g = 3
There will be many csv files in this directory "C:\File directory\Log\".
for example, P1040.csv, P1041.csv, P1042.csv, N1043.csv, P1044.csv, L1045.csv, etc.

The initial letters of the data files vary from P, N and L and these letters refer to the type of data.
i means the serial number of the file.

Among the various types and serial number of data files, there will be only one file type(P,L,N) with the serial number i inside the directory,

for example, if there is one file named P1042.csv (where P is the type and 1042 is the serial number i), then there will be no more N1042.csv or L1042.csv. but of course the next file type will be either P, N or L followed by 1043.csv.

The purpose of this program is to open all the files of the serial number i regardless of the file type.
However, when I tested the program, the program look for all "P", "N" and "L" file type of the same serial number (which does not exist) so for example, if N1043.csv was found, it was opened and returned as Error for P type and L type.

What I want is to open all type of files according to the serial number regardless of the file type.

I hope you all can help me with this.
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 18:34
Joined
Jan 20, 2009
Messages
12,849
Use the FileExists Method of the FileSystemObject before you try to open it.

An alternative would be to use the Dir() function to return the filenames in the folder and test them for the serial number.
 

S(0RP!0N

New member
Local time
Today, 15:34
Joined
Nov 1, 2011
Messages
2
can you give me the codes?
I do not have knowledge of those methods.
 

Users who are viewing this thread

Top Bottom