Excel VBA: Loop Through Every Sheet in a Workbook Safely

VBA & macros · Updated November 21, 2026

To run VBA on every worksheet in a workbook, use a For Each loop with the workbook’s Worksheets collection. In a macro stored in that workbook, the basic pattern is For Each ws In ThisWorkbook.Worksheets; put the action you want to perform between that line and Next ws.

A basic loop through every worksheet

In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, then choose Insert > Module. Paste a procedure like this into the module:

Sub ListWorksheetNames()
    Dim ws As Worksheet

    ' Visit each worksheet in the workbook containing this code.
    For Each ws In ThisWorkbook.Worksheets
        Debug.Print ws.Name
    Next ws
End Sub

Run the procedure from the editor by placing the cursor inside it and pressing F5, or return to Excel and run it from Developer > Macros. The example writes each worksheet’s name to the Immediate window in the Visual Basic Editor; open that window with Ctrl+G if it is not visible. It demonstrates the loop without changing cells, which makes it a useful first check that the macro is targeting the workbook you expect.

On Mac, use Developer > Visual Basic to open the editor; the keyboard shortcut can depend on the keyboard and function-key settings. Excel for the web can open a workbook containing VBA, but it cannot create, edit, or run VBA macros. Use the desktop Excel app for these steps.

How the For Each loop works

ThisWorkbook.Worksheets means the collection of worksheet tabs in the workbook that contains the running VBA code. On each pass, Excel assigns one worksheet object to the variable ws. The statements inside the loop then act on that worksheet. Next ws advances to the following worksheet; when there are no more, execution continues after the loop.

The declaration Dim ws As Worksheet tells VBA what kind of object the variable represents. This is clearer and safer than using a generic variable when every item is a worksheet. A workbook can have zero worksheets in some unusual situations, but in a normal workbook the loop simply runs once per worksheet, regardless of whether a sheet is visible or hidden.

Use the plural collection name deliberately: Worksheets includes worksheet tabs, while Sheets includes both worksheets and chart sheets. If a workbook has a chart sheet, a loop variable declared As Worksheet cannot represent that chart sheet. Choose the collection and variable type to match what you intend to process.

Put a useful action inside the loop

A loop becomes useful when its body refers to the current sheet through ws. For example, this macro bolds cell A1 on every worksheet:

Sub BoldA1OnEveryWorksheet()
    Dim ws As Worksheet

    ' Apply formatting to cell A1 on each worksheet.
    For Each ws In ThisWorkbook.Worksheets
        ws.Range("A1").Font.Bold = True
    Next ws
End Sub

The reference ws.Range("A1") is important: it means cell A1 on the worksheet currently being processed. By contrast, Range("A1") without a worksheet qualifier depends on the active sheet, so it may repeatedly affect the wrong sheet or produce confusing results. Qualifying ranges, cells, and other sheet-specific objects with ws makes the target explicit.

Other common actions include checking a cell, applying a consistent number format, clearing a known input range, or calculating each sheet. Before making a change across many tabs, test the action on a copy or temporarily replace it with a harmless output such as Debug.Print ws.Name. A one-line instruction can affect many sheets very quickly.

Choose the workbook reference carefully

ThisWorkbook and ActiveWorkbook are not interchangeable. ThisWorkbook is the workbook where the macro code is stored. ActiveWorkbook is the workbook currently active in Excel, which may be a different file. A macro in a separate controller file or Personal Macro Workbook may therefore need to target the active workbook or, more reliably, a workbook explicitly selected by the code.

If you intend to process whichever workbook the user has selected, use an explicit workbook variable so the loop’s target is easy to see:

Sub ListActiveWorkbookSheets()
    Dim wb As Workbook
    Dim ws As Worksheet

    ' Capture the active workbook before starting the loop.
    Set wb = ActiveWorkbook
    If wb Is Nothing Then Exit Sub

    For Each ws In wb.Worksheets
        Debug.Print wb.Name & " - " & ws.Name
    Next ws
End Sub

This is safer than repeatedly referring to ActiveWorkbook inside a longer procedure, because the active workbook can change while a macro is running if it opens or activates another file. If you know the exact workbook, you can instead set wb to an open workbook by its name. The name must match the workbook’s actual name, including its file extension, and the workbook must already be open.

Skip sheets or process only selected ones

You do not have to perform the same action on every worksheet. Use an If condition inside the loop to exclude a tab by name, check whether a sheet is visible, or process sheets that meet a condition in their cells. For example, this version leaves a worksheet named Summary unchanged:

Sub BoldA1ExceptSummary()
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        ' Skip the summary tab and format the others.
        If ws.Name <> "Summary" Then
            ws.Range("A1").Font.Bold = True
        End If
    Next ws
End Sub

Sheet names are compared as text, so make sure the name in the condition matches the tab name. A renamed, misspelled, or extra-spaced name may cause the condition to include or exclude a different set of sheets than intended. For a condition based on visibility, test ws.Visible = xlSheetVisible; hidden worksheets remain part of the Worksheets collection.

For a complex rule, keep the decision separate from the action: first test whether the current worksheet qualifies, then run the operation only if it does. This makes the procedure easier to review and reduces the chance of applying a change to an unintended tab.

Common errors and how to avoid them

  • Wrong workbook: A macro using ThisWorkbook may process the file containing the code rather than the data file. Confirm where the code is stored and choose ThisWorkbook, ActiveWorkbook, or a specific workbook variable accordingly.
  • Using the wrong collection: Choose Worksheets for worksheet tabs. Choose Sheets only when chart sheets should be included too, and use a variable type that can represent both kinds of sheet.
  • Unqualified cell references: Write ws.Range("A1") instead of relying on the active sheet. An unqualified range can point somewhere other than the current loop item.
  • Protected sheets or invalid operations: A write or formatting command can fail if a worksheet is protected or the requested operation is not allowed. Identify the specific sheet and operation before adding error handling; suppressing every error can conceal partial results.
  • Unexpected hidden tabs: A normal loop includes hidden and very hidden worksheets. Add a visibility test if the task should apply only to tabs users can see.

When debugging, add Debug.Print ws.Name immediately before the operation. The Immediate window then shows which sheet the macro reached. You can also press F8 in the Visual Basic Editor to step through the procedure one line at a time on Windows. This helps distinguish a loop problem from a problem with the operation inside it.

Run macros with care

Save a separate copy of the workbook before running a macro that changes cells, formatting, formulas, or sheet structure. VBA changes cannot be reversed with Ctrl+Z, even when the change appears small. A loop can repeat an incorrect action across every worksheet before you notice it.

Workbooks that contain VBA should generally be saved in a macro-enabled format such as .xlsm; saving as a macro-free format can remove the VBA project. Excel may display a security warning when a workbook contains macros. Enable macros only when you trust the source and understand what the code does. Do not lower macro security settings broadly just to run an unfamiliar file.

For an initial run, try a copy with a harmless operation, such as printing worksheet names, and confirm the target workbook and included tabs. If the code will alter data, consider limiting it to a clearly identified range and keeping a backup of the original values.

When to use another approach

For a task that must be repeated manually or run on events, VBA is useful because it can work with each worksheet object and perform many kinds of workbook operations. But it is not the only option. If the goal is just to inspect or combine data, Power Query may be a better fit: in desktop Excel for Windows, use Data > Get Data to import and transform data, including data from files or tables, without writing a VBA loop. Power Query is designed for data transformation rather than arbitrary changes to sheet formatting or workbook structure.

For a small workbook, doing the operation manually may be easier to audit than creating a macro. For a large workbook with a consistent repeated task, VBA can save time, provided the workbook reference, inclusion rules, and backup plan are clear. Choose based on what needs to happen, how often it happens, and whether the process needs to be repeatable.

Frequently Asked Questions

What is the VBA code to loop through every sheet?
Declare a worksheet variable, then use For Each ws In ThisWorkbook.Worksheets and put the operation inside the loop. Use ActiveWorkbook.Worksheets or a specific workbook variable if the target is not the file containing the macro.

Does For Each ws In Worksheets include hidden sheets?
Yes. The Worksheets collection includes hidden and very hidden worksheets. Check ws.Visible inside the loop if you want to process only visible tabs.

What is the difference between Sheets and Worksheets in VBA?
Worksheets contains worksheet tabs. Sheets contains worksheet tabs and chart sheets. Use Worksheets for cell-based work on worksheets; use Sheets when chart sheets should also be included.

Why does my macro run on the wrong workbook?
The macro may use ThisWorkbook, which refers to the file containing the VBA, while your data is in another file. Capture the intended workbook in a Workbook variable and loop through that workbook’s Worksheets collection.

Can I run an Excel VBA loop in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in the desktop version of Excel to create or run VBA; the desktop app is available for Windows and Mac, with some interface and shortcut differences.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.