Loop through a list of files

RonEmm

Board Regular
Joined
Aug 18, 2009
Messages
108
Hi,

I am trying to create a small macro that will loop throufh a list of Excel files. All files are in the same folder and the file name allwayst starts with "Item 2010". The rest of the file name varies form 01 04 to 12 31. The complete name of the first file would be "Item 2010 01 04.xls".

So far I have come up with this code. The problem is that the code only picks up the first file and does not continue with the next one (e.g. Item 2010 01 05.xls).

Code:
    MyFolder = "C:\Mydocs\"
    MyFile = Dir(MyFolder & "Item 2010 *.xls")

    Do While MyFile <> ""
        Workbooks.Open Filename:=MyFile
         'Perform other actions
        ActiveWorkbook.Close False
    Loop
Can anyone advise?

Many thanks!
Ron
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try

Code:
    MyFolder = "C:\Mydocs\"
    MyFile = Dir(MyFolder & "Item 2010 *.xls")

    Do While MyFile <> ""
        Workbooks.Open Filename:=MyFolder & MyFile
         'Perform other actions
        ActiveWorkbook.Close False
        MyFile = Dir
    Loop
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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