vba to loop through folder containing xlsx files`

p4nny

Board Regular
Joined
Jan 13, 2015
Messages
246
Hi

I'm looking for some code that will loop through a specified folder (C:\My Documents\) containing xlsx files and

open, calculate and save & close each workbook

If file is read-only, then do nothing and loop to next file.

hope that makes sense

Cheers
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this:
Rich (BB code):
Sub OpenAndCalc()
Dim myDir As String, fn As String, SaveFlag as Boolean

    Application.ScreenUpdating = False

    myDir = "C:\Users\username\Documents\"
    fn = Dir(myDir & "*.xls*")
    
    Do While fn <> ""
        Workbooks.Open (myDir & fn)
        SaveFlag = Not ActiveWorkbook.ReadOnly
        ActiveWorkbook.Close SaveFlag
        fn = Dir()
    Loop
    
End Sub

Change the user name in red to your username.

Let me know how that works.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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