Results 1 to 3 of 3

Thread: How to open files with sequential file name automatically?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    1

    Thumbs down How to open files with sequential file name automatically?


    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!!!!!!!

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. ' While the next file exists open it
    2. Dim nFile As Long, hFile As Long
    3. Dim sNext As String
    4.  
    5. ' sPath is the folder to look in
    6.  
    7. nFile = 1
    8. sNext = sPath & "\MAN" & Format$(nFile, "000")
    9. Do While (LenB(Dir$(sNext)))
    10.     hFile = FreeFile()
    11.     Open sNext For Input As #hFile
    12.         ' Do what you want
    13.     Close #hFile
    14.     nFile = nFile + 1
    15.     sNext = sPath & "\MAN" & Format$(nFile, "000")
    16. Loop

    That's the general idea. I'm not sure how you open files into Excel itself using VBA.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to open files with sequential file name automatically?

    Moved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width