Open All Workbooks in a Folder

Sometimes you may need to go into your folder, open each workbook, edit, save it, close the workbook, and then open the next one. Opening each workbook in a folder or directory is typically a time consuming manual process. This little macro takes care of that annoyance.

Macro Code (VBA Code)

Sub OpenAllWorkbooks()
'Step 1:Declare your variables
    Dim MyFiles As String
'Step 2: Specify a target folder/directory, you may change it.
    MyFiles = Dir("d:\Temp\*.xlsx")
    Do While MyFiles <> ""
'Step 3: Open Workbooks one by one
    Workbooks.Open "d:\Temp\" & MyFiles

    'run some code here
    MsgBox ActiveWorkbook.Name

    ActiveWorkbook.Close SaveChanges:=True

'Step 4: Next File in the folder/Directory
    MyFiles = Dir
    Loop
End Sub

How This Macro Works

In this macro, we use the Dir function. The Dir function returns a string that represents the name of the file that matches what you pass to it.

In this code, we use the Dir function to enumerate through all the .xlsx files in a given directory, capturing each file’s name. Then we open each file, run some code, and finally save and close the file.

  1. Step 1 declares the MyFiles string variable that will capture each file name that is in the enumeration.
  2. In Step 2, the macro uses the Dir function to specify the directory and file type we are looking for. Note that the code here is looking for *.xlsx. This means that only .xlsx files will be looped through. If you are looking for .xls files, you need to change that (along with the directory you need to search). This macro passes any file name it finds to the MyFiles string variable.
  3. Step 3 opens the file, does some stuff (this is where you would put in any macro code to perform the desired actions), and then we save and close the file. In this simple example, we are calling a message box to show each file name as it opens.
  4. The last step of the macro loops back to find more files. If there are no more files, the MyFiles variable will be blank. If that is the case, the loop and macro end.

How to Use This Macro

To use this macro, you can copy and paste it into a standard module:

  1. Activate the Visual Basic Editor by pressing ALT+F11.
  2. Right-click the project/workbook name in the Project window.
  3. Choose Insert -> Module.
    Insert Module
  4. Type or paste the code in the newly created module. You will probably need to change the target folder or file type.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>

2 comments
  1. KF
    Kevin Fay

    Hi: My macro will open the first file in the folder, but then it stops. Can you see why?

    Thank you!

    Kevin Fay

    Sub Looping()
    '
    ' Looping Macro
    ' This macro will open each file in a folder, perform a specified operation, save, close, and repeat for all files in the folder.
    ' Keyboard Shortcut: Ctrl+Shift+L
    '
    'Step 1:Declare your variables
    Dim MyFiles As String
    'Step 2: Specify a target folder/directory, you may change it.
    MyFiles = Dir("C:\Users\fay44\Documents\aa Looping Macro Files\*.xls")
    Do While MyFiles ""
    'Step 3: Open Workbooks one by one
    Workbooks.Open "C:\Users\fay44\Documents\aa Looping Macro Files\" & MyFiles
    'run some code here
    Range("C10").Select
    ActiveCell.FormulaR1C1 = "Hello."
    ActiveWorkbook.Close SaveChanges:=True
    'Step 4: Next File in the folder/Directory
    MyFiles = Dir
    Loop
    End Sub

  2. CD
    Cynthia Deville

    Thank you so much for including descriptive instructions for each step! I am very new to VBA and get completely confused with other examples I found for this process.