Hi everbody!

I am using Excel and I wrote a code using VBA. I have subroutine which open and read text file. I call this subroutine in the master program. The object of the program is to open read a text file(numeric) and make average of the data , make a table and fill the table with the data. However, I want to modify the program so that it can open 5 text file at the same time and process the data. Below I have a code that open and read one file. Could you please give me an advice how to open and read multiply files at the same time Thanks in advance

Filename = Application.GetOpenFilename()
FileNumber = 1

Open Filename For Input As #FileNumber

' Get data, here I call the subrutine that read the data

If DataCount = 0 Then
MsgBox "No data"
Exit Sub
End If

Re: Excel VBA

Jon Peltier

Five files at once, or sequentially

Here are some hints. GetOpenFileName has a multi-select argument which allows the user to select more than one file at a time. The files are returned as an array, so Filename should be declared as a Variant.

Instead of FileNumber = 1 you should use FileNumber = FreeFile, in case there are any other files open.

You could do something like this to open the files:

Dim iFile as Long

Dim FileNumber() As Long

ReDim FileNumber(LBound(Filename) to UBound(Filename))

For iFile = LBound(Filename) to UBound(Filename)

FileNumber(iFile) = FreeFile

Open Filename(iFile) For Input As #FileNumber(iFile)


Now all the files are open, do your stuff, then close them.

- Jon
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. -

Re: Excel VBA


Hi ! Thank you so much Jon Peltier for your fast reply.Honestly I haven't tried the program yet. I will try it and let you know. Many thanks.