Excel VBA Loop Through All Worksheets
In this article
To loop through every worksheet in the current Excel workbook, use a For Each loop over the workbook’s Worksheets collection. For example, the macro below displays each sheet’s name; change the statements inside the loop to perform your own task. Save a copy of the workbook before running any macro, because VBA changes cannot be undone with Ctrl+Z.
Sub ListWorksheetNames()
Dim ws As Worksheet
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 that workbook’s worksheet tabs, and the loop assigns each worksheet in turn to the variable ws. The code between For Each and Next runs once for every worksheet. Here, Debug.Print writes each name to the Immediate window in the VBA editor.
Using Worksheets is deliberate: it targets worksheets, rather than every kind of sheet tab. Excel workbooks can also contain chart sheets, which display charts as their own tabs but are not worksheets. If you specifically need to include chart sheets, use the broader Sheets collection and declare the loop variable as an Excel Sheet object instead.
How to Run the Macro
- Save a copy of the workbook. If the macro needs to be retained in the file, save it as an Excel Macro-Enabled Workbook (
.xlsm). - Open the Visual Basic Editor. In Excel for Windows, press
Alt+F11. On a Mac, useOption+F11(some keyboards requireFnas well), or open the editor from Excel’s Developer tab. - In the editor, choose Insert > Module, then paste the code into the new module.
- Click inside the
Subprocedure and pressF5to run it. You can also return to Excel and run a macro from Developer > Macros.
If the Developer tab is not visible on Windows, enable it through File > Options > Customize Ribbon and select Developer. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros; use the desktop app for these steps.
Choose the Right Workbook and Collection
The workbook reference determines which tabs the macro visits. ThisWorkbook refers to the file that contains the macro. ActiveWorkbook refers to the workbook currently active in Excel, which may be a different file. Choose based on the task rather than assuming they are interchangeable. A macro stored in a personal macro workbook, for example, may need to operate on ActiveWorkbook instead of its own containing file.
For a task that should affect the workbook holding the code, use ThisWorkbook. For a task that should affect whichever workbook the user has selected, use ActiveWorkbook, but check that it is not Nothing before using it in code that may run without an open workbook.
ThisWorkbook.Worksheets: worksheets in the workbook containing the macro.ActiveWorkbook.Worksheets: worksheets in the workbook currently active in Excel.ThisWorkbook.Sheets: all sheet tabs, including worksheets and chart sheets, in the macro’s workbook.
For a workbook with no chart sheets to process, Worksheets is usually the clearest choice. It also makes a variable declared as Worksheet appropriate, providing worksheet-specific properties such as Cells and UsedRange.
Common Tasks Inside the Loop
Replace Debug.Print ws.Name with an operation that applies to each sheet. For example, the following macro writes a label into cell A1 on every worksheet. It changes workbook content, so keep a backup and review the target workbook before running it.
Sub LabelEveryWorksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1").Value = "Reviewed"
Next ws
End Sub
Other common actions include checking a cell, applying formatting, clearing a specified range, or collecting sheet names. The operation should use the loop variable, such as ws.Range("B2"); otherwise, a line that refers to a bare Range("B2") may act on whichever sheet is active rather than the worksheet currently being processed.
You can also skip sheets based on a condition. For example, put an If statement inside the loop to run an action only when ws.Name matches a particular name. Comparing names exactly is useful when a workbook has a known tab to exclude, but remember that sheet names can be changed by users.
Loop by Position or in Reverse
A For Each loop is generally the simplest way to visit all worksheets when you do not need their numeric positions. If you need the worksheet index, or need to delete sheets while looping, a numeric loop may be more suitable. When deleting, loop backward so removing one item does not shift the positions of sheets that have not yet been checked.
Sub DeleteWorksheetsNamedTemp()
Dim i As Long
' Work backward because deletion changes sheet indexes.
For i = ThisWorkbook.Worksheets.Count To 1 Step -1
If ThisWorkbook.Worksheets(i).Name Like "Temp*" Then
Application.DisplayAlerts = False
ThisWorkbook.Worksheets(i).Delete
Application.DisplayAlerts = True
End If
Next i
End Sub
This example deletes every worksheet whose name begins with “Temp.” Deletion is consequential, and suppressing alerts removes Excel’s usual confirmation prompt. Use a backup, verify the naming rule, and restore alerts if an error occurs before the macro reaches the final assignment. For routine work that does not remove tabs, prefer the simpler For Each pattern.
Common Problems and Safe Practices
- Wrong workbook: If the code acts on unexpected sheets, check whether you used
ActiveWorkbookorThisWorkbook. They can refer to different files. - Wrong object type: A variable declared
As Worksheetcannot represent a chart sheet. Use theSheetscollection and a suitable sheet object when chart tabs must be included. - Unqualified references: Use
ws.Range(...)orws.Cells(...)inside the loop. Unqualified references can target the active sheet instead. - Protected sheets: Some edits fail when a worksheet is protected. The macro may need appropriate permissions or to unprotect the sheet before changing it; do not assume protection can be bypassed safely.
- Macro security: Run VBA only from workbooks and sources you trust. Excel may block macros depending on security settings and the file’s source.
For large workbooks, looping through sheets is often straightforward, but repetitive cell-by-cell changes can be slow. Where practical, read or write a range in one operation rather than making many individual calls. If you temporarily change application settings such as screen updating or calculation, restore their original values even if the macro encounters an error.
Frequently Asked Questions
How do I loop through all worksheets in the active workbook?
Use For Each ws In ActiveWorkbook.Worksheets and declare ws As Worksheet. This targets the workbook active when the macro runs, not necessarily the workbook that contains the code.
Does Worksheets include hidden sheets?
Yes. The Worksheets collection includes worksheets that are visible, hidden, or very hidden. A loop visits them unless you add a condition checking ws.Visible.
How do I skip a worksheet by name?
Place an If condition inside the loop that compares ws.Name with the tab name to skip, and run the desired action only when the names do not match.
What is the difference between Sheets and Worksheets?Worksheets contains worksheet tabs only. Sheets contains all sheet tabs, including chart sheets. Use the collection that matches the kinds of tabs your macro must handle.
Can I run a VBA loop in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in the Excel desktop application to edit or run VBA.