KutoolsforOffice β€” One Suite. Five Tools. Get More Done.February Sale: 20% Off

How to run a macro simultaneously across multiple workbooks?

AuthorXiaoyangLast modified

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
This batch-processing method is ideal for users managing repetitive Excel tasks in business, education, or research settings.

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 own Sub and End Sub lines) between this line and End 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:

A screenshot showing the VBA code for running a macro on multiple workbooks in Excel

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.

A screenshot of the folder selection dialog for running a macro on multiple workbooks

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

πŸ€–Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |  Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |  Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

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.

ExcelWordOutlookTabsPowerPoint
  • 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