Excel VBA Loop Through Sheets in a Workbook
In this article
To loop through every worksheet in an Excel workbook, use VBA’s For Each loop with the workbook’s Worksheets collection. For example, the macro below prints each sheet name in the Immediate window; change the line inside the loop to perform the task you need. Save a copy of the workbook before running a macro, because VBA actions cannot be undone with Ctrl+Z.
Sub ListWorksheetNames()
Dim ws As Worksheet
' Visit each worksheet in this workbook.
For Each ws In ThisWorkbook.Worksheets
Debug.Print ws.Name
Next ws
End Sub
What the loop does
ThisWorkbook means the workbook containing the VBA code. Its Worksheets collection contains worksheet tabs, and the loop assigns each worksheet in turn to the variable ws. The code between For Each and Next runs once for each worksheet. In the example, Debug.Print writes the current sheet name to the Immediate window in the VBA editor; it does not change the workbook.
In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module, paste the procedure into the module, and run it with the cursor inside the procedure by pressing F5. If the Immediate window is not visible, choose View > Immediate Window in the editor. On a Mac, open the Visual Basic Editor using the Developer tab or Excel’s Tools > Macro > Visual Basic Editor command, depending on the interface. Keyboard shortcuts can differ by keyboard and Excel version.
Loop through worksheets and make a change
Usually, a loop is useful because it performs the same operation on many tabs. For example, this macro places a label in cell A1 on each worksheet:
Sub AddLabelToEverySheet()
Dim ws As Worksheet
' Write the same value to A1 on every worksheet.
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1").Value = "Reviewed"
Next ws
End Sub
This changes workbook data, so make and save a separate copy first. If some sheets already contain important information in A1, this example will overwrite it. To limit changes, add a condition or target a different cell that is safe to use.
For a task that depends on each sheet’s content, use the worksheet variable to qualify every reference. For instance, with the same ws declaration and loop structure, ws.Range("B2").Value refers to B2 on the current sheet. A reference such as Range("B2") without ws. may instead act on the active sheet, which can produce inconsistent results.
Choose the right workbook and collection
The choice between ThisWorkbook and ActiveWorkbook matters. ThisWorkbook is the file that stores the macro. ActiveWorkbook is whichever workbook is currently active in Excel. If the macro is stored in Personal.xlsb or an add-in but should work on the workbook the user selected, ActiveWorkbook may be appropriate. If the macro should always work on its own file, use ThisWorkbook.
Use Worksheets when you mean ordinary worksheet tabs. A workbook’s broader Sheets collection can also include chart sheets, which contain charts rather than worksheet cells. If you loop through Sheets, declare the loop variable as an object type that accommodates different sheet types, such as Object, and account for the fact that not every item has worksheet properties such as Range.
- All worksheet tabs:
ThisWorkbook.Worksheets - All sheet tabs, including chart sheets:
ThisWorkbook.Sheets - The workbook currently active in Excel:
ActiveWorkbook.Worksheets, when that is genuinely the target
Skip sheets or run code conditionally
Sheet names can be checked inside the loop to exclude a tab. The following example writes a value to B1 on every worksheet except the one named “Summary”:
Sub UpdateExceptSummary()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
ws.Range("B1").Value = "Updated"
End If
Next ws
End Sub
For more than one excluded sheet, combine conditions with And or Or, or use a small helper function. You can also test a cell’s value before changing anything—for example, run an operation only when ws.Range("A1").Value is not blank. When comparisons involve user-entered text, consider capitalization and extra spaces; VBA’s text comparison behavior can depend on how the code is written.
Common problems and safe practices
- Wrong workbook: A macro using
ActiveWorkbookmay target a different file than expected. Confirm which workbook is active, or useThisWorkbookwhen the macro’s own file is the intended target. - Unqualified references: Use
ws.Range(...)andws.Cells(...)inside the loop so operations apply to the current worksheet, not whichever tab happens to be active. - Protected sheets: Writing to a protected worksheet may fail unless the workbook’s protection settings permit the change or the code handles protection appropriately. Do not remove protection without authorization.
- Hidden sheets: The worksheets collection includes hidden and very hidden worksheets. A normal loop visits them too. Check
ws.Visibleif the task should apply only to visible tabs. - Errors partway through: A bad range reference or protected sheet can stop the procedure. Test on a copy and decide how errors should be reported rather than silently ignoring them.
Macros require a macro-enabled workbook format such as .xlsm if you want to retain VBA in the file. Excel may block macros from files downloaded from the internet or from locations that are not trusted. Enable macros only when you trust the workbook and its source. Excel for the web can open many macro-enabled workbooks, but it cannot create, run, or edit VBA macros; use the desktop Excel application for this code.
Frequently Asked Questions
How do I loop through all sheets in Excel VBA?
Declare a worksheet variable and use For Each ws In ThisWorkbook.Worksheets, placing the desired operation before Next ws. Use ActiveWorkbook instead only when the currently active file is the intended target.
What is the difference between Worksheets and Sheets in VBA?Worksheets contains worksheet tabs. Sheets includes worksheets and chart sheets. Choose the collection that matches the task so the code does not assume a chart sheet has worksheet cells.
Does a VBA loop include hidden worksheets?
Yes. A loop over Worksheets visits hidden and very hidden worksheets as well as visible ones. Test ws.Visible inside the loop if you want to skip hidden tabs.
Can I loop through sheets in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel for Windows or Mac to run or edit VBA.
How can I skip one worksheet by name?
Put the operation inside an If ws.Name <> "Summary" Then condition within the loop, replacing “Summary” with the exact tab name to exclude.