How to open files with sequential file name automatically?
:confused:
I try to use Excel to deal with some calculation.
I need to open one txt file, input the data into Excel spreadsheet, FFT, save result, close the txt file; then open the second txt file, repeat the process, then the third.....
All the txt files have sequential file name, say MAN001, MAN002, .....MAN270.
I don't know how to integrate the LOOP process with the open command to open those series of files and close them automatically. Say I want to analyze 5 files from MAN130.
Please Help!!!!!!!
Re: How to open files with sequential file name automatically?
Is this VBA (VB in Excel) or standalone VB? I guess it is VBA..
Anyway, you can use a For...Next or Do While loop to open the files in sequence.
VB Code:
' While the next file exists open it
Dim nFile As Long, hFile As Long
Dim sNext As String
' sPath is the folder to look in
nFile = 1
sNext = sPath & "\MAN" & Format$(nFile, "000")
Do While (LenB(Dir$(sNext)))
hFile = FreeFile()
Open sNext For Input As #hFile
' Do what you want
Close #hFile
nFile = nFile + 1
sNext = sPath & "\MAN" & Format$(nFile, "000")
Loop
That's the general idea. I'm not sure how you open files into Excel itself using VBA.
Re: How to open files with sequential file name automatically?