Iterating through every worksheet in a workbook is one of the most powerful VBA patterns. For Each ws In Worksheets visits every sheet automatically. You can apply formatting, copy data, rename sheets, or protect them — all in a single loop. It works regardless of how many sheets the workbook has.
This guide covers two main loop patterns and six practical examples. These include printing all sheets, clearing data, copying a value to all sheets, renaming from a list, hiding all but one sheet, and collecting data from all sheets into a summary.
Worksheets iterates only worksheet tabs. Sheets iterates all tab types including Chart sheets and Dialog sheets. In most cases, use Worksheets to avoid processing non-data tabs accidentally.Two Loop Patterns Compared
There are two standard patterns for looping through sheets. For Each is cleaner and does not require an index. For i is useful when you need the sheet number for alignment with a list in another sheet.
Examples 1–4: Worksheet Loops in Practice
This macro loops through every sheet and calls PrintOut on each one. An If statement skips the sheet named "Cover". Similarly, you can skip any sheet by adding its name to the condition.
Clearing a specific range on every data sheet is a common template-reset task. ClearContents removes values but leaves cell formatting intact. The loop skips the sheet named "Template" to preserve the master copy.
When a report date in cell A1 must appear on every sheet, a loop is the most reliable approach. The macro reads the value from the Summary sheet and writes it to A1 on every other sheet. This keeps all sheets synchronised without manual copy-paste work.
When column A on a "Names" sheet contains the new tab names, this macro renames sheets sequentially using a For i loop. The sheet index aligns with the row number. Sheet names cannot contain characters like / \ * ? : [ ].
Examples 5–6: Hide Sheets and Collect Summary Data
Before sharing a file, it is common to hide all sheets except the dashboard. This macro hides every sheet except "Summary" using xlSheetVeryHidden, so users cannot unhide them via the right-click menu. A second macro restores all sheets when needed.
This macro reads cell B2 from every sheet and writes it to the Summary sheet as a new row. The result is an automatic data consolidation that replaces manual copy-paste from each individual sheet. The Summary range is cleared first to avoid duplicate rows on repeated runs.
Common Issues and How to Fix Them
The loop skips a sheet or processes the wrong one
This usually happens when sheets are added or deleted during the loop. For Each is safe because it iterates a snapshot of the collection. However, For i = 1 To Count can skip sheets if you delete a sheet inside the loop, because the Count changes. In that case, loop backward using Step -1 to avoid index-shift errors.
Run-time error 1004 when writing to a protected sheet
The loop is trying to write to a locked cell on a protected sheet. Add an Unprotect call at the start of each iteration and a matching Protect call at the end. Alternatively, protect all sheets using UserInterfaceOnly:=True so VBA can always write without toggling protection inside the loop.
Frequently Asked Questions
-
How do I loop through all worksheets in VBA?+Use For Each ws In ThisWorkbook.Worksheets. Declare ws As Worksheet at the top of the Sub. Inside the loop, ws refers to the current sheet on each iteration. To skip specific sheets, add an If ws.Name <> "SheetName" Then condition inside the loop body.
-
What is the difference between Worksheets and Sheets in VBA?+Worksheets refers only to standard grid-based worksheet tabs. Sheets refers to all tab types in the workbook, including Chart sheets, Macro sheets, and Dialog sheets. In most looping scenarios, use Worksheets to avoid processing non-data tabs accidentally. Use Sheets only when you specifically need to include Chart sheets or other special tab types.
-
How do I skip a specific sheet inside the loop?+Add an If condition inside the loop body. For example: If ws.Name <> "Summary" Then ... End If. To skip multiple sheets, use And: If ws.Name <> "Summary" And ws.Name <> "Cover" Then. This skips both named sheets and processes all others. Alternatively, store excluded sheet names in an array and use a helper function to check membership.
-
How do I loop through only visible sheets?+Check the Visible property inside the loop: If ws.Visible = xlSheetVisible Then. This skips hidden sheets (xlSheetHidden) and very hidden sheets (xlSheetVeryHidden). Only visible tabs are processed. This is useful when some sheets are hidden for distribution but should not be modified by bulk macros.