Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Loop Through Each Sheet in a Workbook

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Worksheets vs Sheets: Which Collection to Loop
  4. Skipping Hidden or Very Hidden Sheets
  5. Common Mistakes
  6. Tips
  7. Troubleshooting
  8. Frequently Asked Questions

To loop through every sheet in an Excel workbook with VBA, use a For Each loop over the Worksheets collection: Dim ws As Worksheet, then For Each ws In ThisWorkbook.Worksheets … Next ws. Inside the loop, ws refers to the current sheet, so you can read, write, format, or export it without ever naming the tab. That is the whole trick: the loop handles the sheet references, and you supply the action.

The pattern matters because hard-coding sheet names breaks the moment someone renames a tab or inserts a new one. A collection loop keeps working. This guide covers the exact syntax, the difference between Worksheets and Sheets, how to skip hidden or chart sheets, and the errors that trip people up most often.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm if you want the macro to persist after closing.
  • Basic familiarity with the VBA editor: press Alt+F11 on Windows, or Fn+Option+F11 on Mac, to open it.
  • A test workbook with at least two sheets so you can see the loop act on more than one.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). The editor opens in its own window.
  2. Insert a module. In the Project pane, right-click your workbook name, choose Insert > Module. A blank code window appears. Macros written in a standard module are available to the whole workbook.
  3. Start the subroutine. Type Sub LoopSheets() and press Enter. The editor adds End Sub automatically.
  4. Declare a worksheet variable. On the next line type Dim ws As Worksheet. Using a typed variable gives you IntelliSense and catches typos at compile time rather than at runtime.
  5. Open the loop. Type For Each ws In ThisWorkbook.Worksheets. ThisWorkbook means the workbook containing the code; use ActiveWorkbook only if you deliberately want whichever workbook is in front.
  6. Add the action. Between the For Each and Next lines, write what should happen to each sheet. A safe first test is Debug.Print ws.Name, which writes each tab name to the Immediate Window (Ctrl+G).
  7. Close the loop. Type Next ws on its own line, then End Sub below it.
  8. Run it. Press F5 with the cursor inside the subroutine. Open the Immediate Window to confirm every sheet name was printed, including hidden ones.
  9. Replace the test action with real work. Common replacements: ws.Range("A1").Value = "Reviewed" to stamp a cell, ws.Cells.ClearContents to wipe data, or ws.UsedRange.Columns.AutoFit to resize columns. Keep the loop structure identical; only the middle line changes.
  10. Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx will silently drop the code.

Labels and menu positions vary slightly by Excel version, platform, and region, so if a ribbon item is not where described, look for the closest equivalent.

Worksheets vs Sheets: Which Collection to Loop

Worksheets contains only worksheet objects. Sheets contains worksheets plus chart sheets, and in some workbooks also macro or dialog sheets. If your code touches cells, loop Worksheets; a chart sheet has no Range and will throw error 438 (object doesn't support this property or method). Loop Sheets only when you genuinely need to process chart sheets too, and branch on the object type inside the loop.

Skipping Hidden or Very Hidden Sheets

Hidden sheets are still members of the collection, so a plain loop will process them. To skip them, add an If ws.Visible = xlSheetVisible Then block around your action and close it with End If before Next ws. Sheets with xlSheetVeryHidden status do not appear in the right-click menu and can only be unhidden through VBA or the Properties pane, so guard against them if your macro writes to every sheet.

Common Mistakes

  • Using ActiveSheet inside the loop. The active sheet does not change as the loop runs, so every iteration acts on the same tab. Always use the loop variable ws.
  • Forgetting ws. before a range. Writing Range("A1") without a sheet qualifier hits the active sheet, not the current one.
  • Looping Sheets but calling cell methods. Chart sheets cause runtime error 438. Switch to Worksheets or add a type check.
  • Deleting sheets inside a For Each loop. Removing an item from a collection while enumerating it can skip sheets or fail. Collect the names first, then delete in a separate pass, or loop backwards with an index.
  • Assuming ThisWorkbook is the file you see. If your macro lives in a personal macro workbook or an add-in, ThisWorkbook points there. Use ActiveWorkbook or capture the workbook in a variable before the loop.
  • Saving as .xlsx. The code vanishes on close with no warning beyond a one-time prompt.

Tips

  • Turn off Application.ScreenUpdating = False before a long loop and set it back to True afterwards; this speeds up visible changes dramatically. Add Application.DisplayAlerts = False only if you are sure no dialog should interrupt.
  • Use ws.Index when you need the sheet's position, and ws.Name for the tab text.
  • If you only want a subset, test the name inside the loop: If InStr(1, ws.Name, "Data", vbTextCompare) > 0 Then processes only tabs containing "Data".
  • Store results in a variable or write them to a summary sheet rather than relying on Debug.Print, which is cleared whenever the editor resets.
  • Keep the action line short and call a separate subroutine for complex work; the loop stays readable and easy to reuse.

Troubleshooting

Nothing happens when you press F5. The cursor may be outside the subroutine. Click inside Sub … End Sub and run again, or choose Run > Run Sub/UserForm and pick the macro by name.

Compile error: Variable not defined. Either the Dim line is missing or Option Explicit is on and a name is misspelled. Check the spelling of ws in every reference.

Runtime error 438. You are calling a worksheet-only method on a chart sheet, or the collection does not support the property. Switch to Worksheets or guard with a type check.

Runtime error 1004. Usually a protected sheet or workbook, or an invalid range reference. Unprotect the sheet first, or check that the range exists on every tab.

The macro runs on the wrong workbook. Replace ThisWorkbook with ActiveWorkbook, or set Dim wb As Workbook: Set wb = Workbooks("Book1.xlsx") and loop wb.Worksheets.

It stops partway through. A hidden or protected sheet is likely throwing an error. Step through with F8 to find the exact sheet, then add a visibility or protection check.

Frequently Asked Questions

What is the difference between For Each ws In Worksheets and For Each ws In ThisWorkbook.Worksheets?

The first refers to the active workbook's worksheets; the second explicitly targets the workbook containing the code. They behave the same when the code lives in the file you are viewing, but differ when the macro is stored elsewhere.

How do I loop through sheets in a different, closed workbook?

Open it first with Workbooks.Open, loop its Worksheets collection, then close it. VBA cannot read a closed workbook's sheets directly.

Can I loop through sheets in alphabetical order?

Not directly with For Each, which follows tab order. Either sort the tabs physically or build an array of names, sort that array, and loop the array instead.

Why does my loop skip the first sheet?

It usually does not; the first sheet is often hidden or the action writes somewhere you are not looking. Print ws.Name for every iteration to confirm.

Does this work on Excel for Mac?

Yes. The object model and syntax are the same; only the keyboard shortcuts for opening the editor differ.

How do I stop the loop early?

Use Exit For inside an If block. Execution jumps to the line after Next ws.