Opening text files sequentially in Excel macro

Hchristian_314

New Member
Joined
Oct 5, 2017
Messages
1
I have multiple text files that I want to open in Excel, but only one at a time. I can do this with a macro successfully for the first file in the directory, but how do I get the macro to open the next file each time I run It? Alternatively I could loop the macro to do them all in one execution sequence, however I would rather not do it that way, as there are a number of things I need to do to each file's contents after opening.

Thanks,
Harry
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Harry

This code may help. It loops through all text files in a given directory and prints the name in the immediate window. You should be able to alter it to suit your needs

Code:
 Sub LoopFiles()

    Dim sFileName As String
    Dim sPath As String
    Dim sFilter As String
        
    'Path of Files
    sPath = "C:\Test\"
    
    'Filter to only get txt files
    sFilter = "*.txt"
    
    'Get first filename
    sFileName = Dir(sPath & sFilter)
    
    'If filename isn't blank then send print to immediate iwndow and get next filename then repeat
    Do While sFileName <> ""
        Debug.Print sFileName
        'Get Next File
        sFileName = Dir
    Loop
    
 End Sub

Naturally you will need to change the sPath variable to suit
 
Last edited:
Upvote 0
Similar to gallen
Code:
Sub GetFileNames()

    Dim FileNme As String
    Dim FilePth As String
        
    FilePth = "C:\Users\Fluff\Documents\Excel files\"
    
    FileNme = Dir(FilePth & "*.xls")
    
    Do While FileNme <> ""
        With Sheets("Files").Range("A" & Rows.Count).End(xlUp)
            .Offset(1).Value = FilePth
            .Offset(1, 1) = FileNme
        End With
        FileNme = Dir
    Loop
    
 End Sub
This will list the file path & file name in a sheets called Files.
Then you can do
Code:
Sub DoSomething()

    Dim FileNme As String
    Dim FilePth As String

    FilePth = ActiveCell
    FileNme = ActiveCell.Offset(, 1)
    
    Workbooks.Open FilePth & FileNme

End Sub
Select cell in col A for the file you want to open & then run the above
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top