Excel VBA: Loop Through All Sheets in a Workbook Safely

VBA & macros · Updated November 21, 2026

To loop through every worksheet in a workbook with Excel VBA, use a For Each loop over that workbook’s Worksheets collection. For example, set a workbook variable to ThisWorkbook, then run For Each ws In wb.Worksheets and place the task you want to perform inside the loop.

A basic loop through every worksheet

This macro prints the name of each worksheet in the workbook that contains the code. It visits worksheets in their tab order and does not change cell contents.

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

    Set wb = ThisWorkbook

    ' Print each worksheet name to the Immediate window.
    For Each ws In wb.Worksheets
        Debug.Print ws.Name
    Next ws
End Sub

In the Visual Basic Editor, open the Immediate window from View > Immediate Window to see the printed names. This is a useful first test: it confirms which sheets the loop reaches before you add code that edits or formats data. A For Each loop is usually clearer than counting from 1 to the number of sheets because it gives you a worksheet object on each pass.

Choose the workbook the macro should process

The workbook reference matters as much as the loop. ThisWorkbook means the workbook that contains the VBA code. If the macro is stored in the workbook being processed, this is often the safest choice because it does not depend on which file happens to be active.

ActiveWorkbook means the workbook currently active in Excel. It can be appropriate when a macro is stored elsewhere—such as in the Personal Macro Workbook—and is intended to work on the workbook the user selected. But focus can change, so an active-workbook macro can target the wrong file. Choose the reference deliberately rather than substituting one for the other without checking.

You can also set wb to a specific open workbook, for example by referring to its name in the Workbooks collection. That approach is explicit, but the name must match an open workbook. If the file is not open or the name is misspelled, VBA raises an error. In all cases, use the same workbook variable in the loop so the target stays clear.

Worksheets and Sheets are not the same collection

For tasks involving cells, use Worksheets. It contains worksheet tabs, including worksheets that are hidden or set to very hidden. A typed variable such as ws As Worksheet makes it clear that the code expects a worksheet with cells, ranges, and rows.

The broader Sheets collection can include chart sheets as well as worksheets. A chart sheet is a separate tab devoted to a chart, not a grid of cells. If a loop uses Sheets but assumes every item has a worksheet’s Range property, it can fail when it reaches a chart sheet. Use Sheets only when the task genuinely needs to inspect every sheet tab, including chart sheets, and handle each sheet type appropriately.

A common misunderstanding is that “all sheets” always means every visible tab. A loop over Worksheets also reaches hidden worksheets. If the macro should operate only on visible worksheets, test the Visible property inside the loop; do not assume hidden sheets are automatically skipped.

Put the macro in a module and run it

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, or choose Developer > Visual Basic. In the editor, use Insert > Module, paste the procedure into the new module, and run it with the cursor inside the procedure by pressing F5 or choosing Run Sub/UserForm. If the Developer tab is not shown, it can be enabled through File > Options > Customize Ribbon.

On Excel for Mac, open the editor through Tools > Macro > Visual Basic Editor, or use Option+F11 where the keyboard provides that shortcut; some keyboards also require Fn. Excel for the web does not run VBA macros, so use the desktop Excel application to edit or execute this code. If you want to keep VBA in the workbook, save it in a macro-enabled format such as .xlsm; saving as .xlsx does not preserve VBA code.

Before running a macro that changes a workbook, save a separate copy. VBA changes generally cannot be reversed with Ctrl+Z, so a backup is the practical way to recover from an unintended edit. Follow your organization’s macro-security policy and run code only when you trust its source.

Put the worksheet task inside the loop

The loop supplies a different worksheet object to ws on each pass. Any worksheet-level operation that uses ws can therefore be repeated across the workbook. For example, ws.Name reads the current tab name, while ws.Range("A1").Value refers to cell A1 on that particular sheet. Qualifying the range with ws is essential: an unqualified Range("A1") can refer to the active sheet instead, which may not be the sheet the loop is processing.

For a simple reporting task, replace the line inside the loop with code that reads a value from each worksheet and records it somewhere. For a formatting task, call a formatting method on a range qualified with ws. For a data-cleaning task, test the relevant cells first, then make the change only when the condition is met. The specific operation depends on the workbook; the loop itself does not decide which cells are appropriate to change.

When a task should apply only to certain sheets, use an If condition inside the loop. You might check a sheet’s name, visibility, or a known marker cell before continuing. This is safer than running a broad edit everywhere, especially when sheets have different layouts. If you refer to a marker cell, confirm that it exists and has the expected meaning on every sheet you intend to include.

Common problems and how to avoid them

  • The macro processes the wrong file: Check whether the code uses ThisWorkbook or ActiveWorkbook, and confirm that the chosen reference matches your goal.
  • A chart tab causes an error: Use Worksheets for cell-based work, or identify chart sheets separately if you loop over Sheets.
  • Hidden tabs are unexpectedly changed: Remember that hidden and very hidden worksheets are in Worksheets. Add a visibility check if they should be excluded.
  • Only the active sheet appears to change: Qualify each range with the loop variable, such as ws.Range("A1"), instead of using an unqualified range.
  • The code stops on a protected sheet: Some edits are blocked by worksheet protection. Decide whether protection should be handled, and do not add password-unprotect code unless you are authorized and know the workbook’s protection requirements.

Also avoid adding or deleting worksheets while iterating through the same collection unless the logic has been designed for that change. Altering a collection during a loop can make the iteration harder to reason about. A safer pattern is to first collect the sheets to process, or to perform structural changes in a separate pass.

Make large loops safer and more efficient

For a workbook with many worksheets or a task that touches many cells, keep the work inside the loop narrowly scoped. Avoid repeatedly selecting tabs or activating cells: most VBA operations can address a range directly through ws. Direct references are typically easier to follow and less dependent on what the user has clicked.

Test a new macro on a copy and, where possible, start with a harmless operation such as printing sheet names. Then test any edits on a small, representative set of sheets. Check the results on hidden sheets, protected sheets, and sheets with unusual layouts if they are included. Error handling can be useful in a production macro, but it should report or deliberately handle a problem rather than silently skip sheets; suppressing every error can conceal incomplete results.

For operations that update a large amount of data, developers sometimes temporarily adjust Excel settings such as screen updating or calculation. Those settings should be restored even if the macro encounters an error. For a basic loop, it is usually better to keep the code simple until there is a measurable reason to add that extra control.

When VBA is not the best option

A VBA loop is useful when the same custom action must be applied across multiple worksheets—for example, producing a tailored report, checking sheet-specific cells, or applying a consistent change. For a one-time task on a few tabs, selecting the relevant worksheets and making the change directly may be quicker. For repeated data consolidation, Power Query may be a better fit when the sheets have compatible tables and the goal is to combine or transform data rather than execute custom actions on each tab.

VBA is a desktop Excel feature. A workbook containing a macro can be opened in Excel for the web, but the web app does not execute VBA. If the workflow must run in a browser, consider an alternative supported by the environment, such as Office Scripts where available, and check that it can perform the required task before changing the design.

Frequently Asked Questions

Does a VBA loop through Worksheets include hidden sheets?
Yes. ThisWorkbook.Worksheets includes visible, hidden, and very hidden worksheets. Check each sheet’s Visible property inside the loop if you want to process only visible tabs.

How do I loop through every tab, including chart sheets?
Use the workbook’s Sheets collection rather than Worksheets. Because that collection can contain chart sheets, write the loop to distinguish chart sheets from worksheets before trying to access cell ranges.

Should I use ActiveWorkbook or ThisWorkbook?
Use ThisWorkbook when the macro should process the file containing its code. Use ActiveWorkbook only when the intended target is specifically the workbook currently selected in Excel.

Can I run this macro in Excel for the web?
No. Excel for the web does not run VBA. Open the workbook in the Excel desktop application on Windows or Mac to run the macro.

Can Ctrl+Z undo changes made by a VBA macro?
Usually not. Excel’s regular undo history is generally cleared when VBA changes workbook content. Save a separate copy before running a macro that edits data or formatting.

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.