How to run a macro simultaneously across multiple workbooks?
Dealing with multiple Excel workbooks often requires repeating the same operation or automation task across several files. Opening each workbook one by one to run a macro not only consumes significant time but also increases the risk of error, especially in scenarios where large batches of files are involved. Automating this process is essential for improving productivity and accuracy.
By leveraging VBA (Visual Basic for Applications), you can streamline your workflow to execute a macro automatically on every workbook in a designated folder, saving effort and ensuring consistency. This guide outlines how to set up and use a VBA solution so you can run your desired macro across multiple workbook files without the need to open each file manually.
Typical scenarios where this approach is valuable include:
- Applying formatting or calculations to a collection of monthly reports
- Standardizing data cleaning steps across a set of data exports
- Batch-updating templates or adding structures to legacy files
Run the same macro across multiple workbooks with VBA code
To efficiently execute the same macro across several workbooks, without manually opening each one, you can use a VBA script designed for batch operations. This method will prompt you to choose a folder and then automatically process each workbook file inside. It is suitable for most common macro automation tasks.
Before you start:
- All target workbooks are closed. The script will open and process them as required.
- The macro you wish to run does not require user interaction (for example, message boxes or input prompts), as batch processing will be interrupted by dialogs.
- You have saved your work, as batch macro processing cannot easily be undone and may overwrite data if not coded carefully.
Follow these steps to apply a macro across multiple workbooks:
1. Hold down the "ALT + F11" keys to open the "Microsoft Visual Basic for Applications" window.
2. In the opened VBA editor, click "Insert" > "Module" to create a new module, then paste the provided macro into the blank module window.
VBA code: Run the same macro on multiple workbooks at the same time:
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'your code here
End With
xFileName = Dir
Loop
End If
End Sub
Important notes and tips:
- In the code above, locate the line
With Workbooks.Open(xFdItem & xFileName). You must copy and insert your desired macro code (excluding its ownSubandEnd Sublines) between this line andEnd With. This allows the operation to execute inside each opened workbook. - Double-check your macro logic and paths to ensure correct batch processing, and consider testing on a small set of files first if your macro makes data changes.
See screenshot illustrating where to paste your macro logic:

3. Press the "F5" key in the VBA editor to run the script. A folder selection dialog will appear.
4. In the "Browse" window, select the folder containing all workbooks you wish to process. The script will loop through each Excel file found in that location and run your macro logic.

5. Click "OK" to start the batch operation. The macro section you added will be executed across all targeted workbooks in the chosen folder. Larger batches may take longer, depending on the complexity of your macro and the number of files.
- Macro security: If prompted, enable macros or run from a trusted location.
- Target file types: The pattern
*.xls*includes.xlsx,.xlsm, and.xlsb. Adjust if needed. - Fully qualify inside With: Use leading dots (e.g.,
.Worksheets("Sheet1")) so actions apply to the opened workbook.
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
All Kutools add-ins. One installer
Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.
- All-in-one suite β Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
- One installer, one license β set up in minutes (MSI-ready)
- Works better together β streamlined productivity across Office apps
- 30-day full-featured trial β no registration, no credit card
- Best value β save vs buying individual add-in