Excel VBA Loop Through Worksheets in a Workbook
In this article
To loop through every worksheet in an Excel workbook, use a For Each loop with the workbook’s Worksheets collection. For example, For Each ws In ThisWorkbook.Worksheets visits each worksheet in the workbook containing the macro; put the action you want inside the loop.
A basic worksheet loop
In Microsoft Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste in a macro like this. Save a copy of the workbook before running it: macros cannot be undone with Ctrl+Z.
Sub LoopThroughWorksheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Debug.Print ws.Name
Next ws
End Sub
This example prints each worksheet’s name in the Immediate window in the Visual Basic Editor. Open that window with Ctrl+G. The macro does not change the workbook; it demonstrates how the loop reaches each sheet. Replace Debug.Print ws.Name with the task you need to perform.
On a Mac, open the Visual Basic Editor using the Developer tab or the applicable Visual Basic Editor command in Excel’s Tools menu; keyboard shortcuts can vary by keyboard and Excel version. Excel for the web can open workbooks containing macros, but it cannot create, edit, or run VBA macros. Use the desktop app for VBA.
What the loop visits
ThisWorkbook means the workbook that contains the running VBA code. Its Worksheets collection contains worksheet tabs, including hidden and very hidden worksheets. A loop through that collection does not include chart sheets, which are separate sheets containing charts rather than worksheet cells.
The variable ws represents one worksheet at a time. On each pass, VBA assigns the next worksheet in the collection to ws, runs the statements between For Each and Next, and then advances. Declaring it as Worksheet makes it clear that the code expects worksheets and gives access to worksheet properties and methods.
Use Worksheets when the task concerns cells, ranges, or worksheet-specific features. Use Sheets if you deliberately need to include all sheet types, such as chart sheets, and declare the loop variable as Object or another suitable type. Code that assumes every sheet has a cell range will not work safely on a chart sheet.
Choose the right workbook reference
The workbook reference determines which file the loop acts on. This distinction matters when a macro is stored in the Personal Macro Workbook, an add-in, or a separate controller workbook.
ThisWorkbook: The file containing the VBA code. Choose this when the macro should work on its own workbook.ActiveWorkbook: The workbook currently active in Excel. Choose it only when the user’s selected workbook is intentionally the target; it can change as the macro runs or another workbook is activated.- A named workbook variable: Useful when the macro opens or selects a particular file. Assign the intended workbook to a variable, then loop through that variable’s
Worksheetscollection.
For example, if code stored in an add-in should process the workbook the user has open, ActiveWorkbook may be appropriate. If the code should always process the workbook where it resides, use ThisWorkbook. Do not assume these names refer to the same file.
Common tasks inside the loop
Once the loop is working, place a worksheet operation inside it. This example clears contents in the range A2:D100 on every worksheet. It removes values and formulas in that range but leaves cell formatting in place.
Sub ClearDataOnEachWorksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A2:D100").ClearContents
Next ws
End Sub
Always confirm the workbook reference and target range before running a change-making macro. If sheet layouts differ, a fixed range may be inappropriate. You can also use a condition to process only sheets that meet a requirement, such as a specific name or a value in a particular cell.
For instance, this example checks the value in A1 before printing the sheet name. It uses Value to read the cell and a text comparison to decide whether to act.
Sub ProcessMatchingWorksheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("A1").Value = "Report" Then
Debug.Print ws.Name
End If
Next ws
End Sub
Skip hidden sheets or handle errors
Because Worksheets includes hidden worksheets, add a visibility test if the task should apply only to sheets users can see. A worksheet’s Visible property is xlSheetVisible when it is visible. Hidden and very hidden sheets have other values, so testing for the visible constant is clearer than trying to identify only one kind of hidden sheet.
Sub ListVisibleWorksheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
Debug.Print ws.Name
End If
Next ws
End Sub
Some operations can fail on protected sheets, missing ranges, or sheets with different layouts. Avoid using On Error Resume Next across an entire loop to conceal problems: it can silently skip failures and leave results incomplete. Prefer checking the relevant condition first, or use a focused error handler that reports which sheet caused the error.
Loop order and alternatives
A For Each loop is usually the simplest choice when every worksheet should receive the same operation. It follows the worksheets’ collection order, which normally corresponds to their tab order, but it does not require you to know how many worksheets exist. If you need to refer to sheets by position, use an indexed loop instead:
Sub LoopByWorksheetIndex()
Dim i As Long
For i = 1 To ThisWorkbook.Worksheets.Count
Debug.Print ThisWorkbook.Worksheets(i).Name
Next i
End Sub
An indexed loop is useful when the sheet number itself matters or when code must move between neighboring sheets. For ordinary processing, a For Each loop is often easier to read and less likely to rely on a count that changes. Avoid deleting or adding worksheets during a collection loop unless you have designed for the collection changing; changing items while iterating can make the sequence harder to control.
Frequently Asked Questions
Does a VBA loop include hidden worksheets?
Yes. A loop through ThisWorkbook.Worksheets includes visible, hidden, and very hidden worksheets. Check ws.Visible against xlSheetVisible if you only want visible tabs.
How do I loop through every sheet, including chart sheets?
Use the workbook’s Sheets collection rather than Worksheets. Because that collection can contain chart sheets as well as worksheets, write the code to account for different sheet types instead of assuming every item has worksheet cells.
What is the difference between ThisWorkbook and ActiveWorkbook?ThisWorkbook is the workbook containing the macro. ActiveWorkbook is whichever workbook is currently active in Excel. Pick the reference based on the intended target, especially when a macro is stored outside the workbook it processes.
Can I run VBA in Excel for the web?
No. Excel for the web does not run VBA macros. Open the file in the Excel desktop app to create, edit, or run VBA.
Can I undo a macro with Ctrl+Z?
No. Excel’s normal undo history does not reverse VBA changes. Save a separate copy before running a macro that edits, clears, moves, or deletes workbook content.