loop through files in dir

detlion1643

Board Regular
Joined
Nov 25, 2009
Messages
164
I have some code that takes a column and compares it to a column in a backup file. The code I have works if i specify the workbook to open. I already have code that gets the filename of the currently open workbook. What I am looking for is a way to loop through each .xls file in a directory. Basically open a file, get filename (already have), run code (already have), then close file.

Code:
For each *.xls in *
get filename
run code
close file

I've never been good with fso's or the bunch.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I use this to close file, where I assign the file a name (bFilename)

Workbooks(bFilename).Close
 
Upvote 0
Try something like

Code:
Sub ProcessFiles()
Dim MyFolder As String
Dim MyFile As String
Dim wb As Workbook
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
    Set wb = Workbooks.Open(MyFolder & "\" & MyFile)
    '
    'code here
    '
    wb.Close
    MyFile = Dir
Loop
End Sub
 
Upvote 0
Awesome Vog, working great.... one more thing if you don't mind. Is there a way to only open files which have a certain string in the name? EX) aa.xls, dg Maps.xls, df.xls, fh Maps.xls to only open the ones with Maps in the filename?
 
Upvote 0
Try

Code:
Sub ProcessFiles()
Dim MyFolder As String
Dim MyFile As String
Dim wb As Workbook
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
    If MyFile Like "*Maps*" Then
        Set wb = Workbooks.Open(MyFolder & "\" & MyFile)
        '
        'code here
        '
        wb.Close
    End If
    MyFile = Dir
Loop
End Sub
 
Upvote 0
In about ten days I will have a need to do the same thing to about 30 *.xls files regularly. This will be the perfect way to loop through those 30 files regularly and run the same macro on each file. Glad you posted this.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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