You can use the following methods to loop through sheets in an Excel workbook using VBA:
Method 1: Loop Through All Worksheets
Sub LoopSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1").Value = 100
Next ws
End Sub
This particular macro loops through each sheet in a workbook and sets the value in cell A1 of each sheet to be equal to 100.
Method 2: Loop Through All Worksheets, Excluding Specific Ones
Sub LoopSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case Is = "Sheet2", "Sheet3"
'Do not execute any code for these sheets
Case Else
ws.Range("A1").Value = 100
End Select
Next ws
End Sub
This particular macro loops through each sheet in a workbook and sets the value in cell A1 of each sheet to be equal to 100, except for the sheets called Sheet2 and Sheet3.
The following examples show how to use each of these methods in practice with an Excel workbook that contains four empty sheets:

Example 1: Loop Through All Worksheets
We can use the following macro to set the value in cell A1 of each sheet in our workbook to be equal to 100:
Sub LoopSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1").Value = 100
Next ws
When we run this macro, the value of cell A1 in each sheet in the workbook will be equal to 100:

Example 2: Loop Through All Worksheets, Excluding Specific Ones
Suppose we would like to loop through each worksheet and set the value of cell A1 in each sheet to be equal to 100, except for the sheets called Sheet2 and Sheet3.
We can create the following macro to do so:
Sub LoopSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case Is = "Sheet2", "Sheet3"
'Do not execute any code for these sheets
Case Else
ws.Range("A1").Value = 100
End Select
Next ws
End Sub
When we run this macro, we will see that both Sheet1 and Sheet4 have a value of 100 in cell A1.
However, Sheet2 and Sheet3 will not have any value in cell A1 since we used the Case function to skip over these sheets:

Note that in these examples we looped through each worksheet and set the value of one cell equal to a specific value just for simplicity’s sake.
However, you can use similar syntax with a For Each statement to perform much more complicated tasks in each sheet if you’d like.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
VBA: How to Count Number of Rows in Range
VBA: How to Count Cells with Specific Text
VBA: How to Write COUNTIF and COUNTIFS Functions
Hi Zach,
As an extension to your article, I have a workwook with about 50 sheets. I want to loop through Sheets 30 to 40 and perform the same routine on each sheet.
How do I set up the basic loop for those 11 sheets.
Any help would be appreciated.
Thanks,
Roger
Hi Roger,
Here’s how you can loop through sheets 30 to 40 in your workbook using VBA. You can use the `Worksheets` collection and specify the range of indexes you want to loop through. Below is the basic code to set up the loop and perform a routine on those specific sheets:
“`vba
Sub LoopThroughSpecificSheets()
Dim ws As Worksheet
Dim i As Integer
‘ Loop through sheets 30 to 40
For i = 30 To 40
‘ Set the current sheet to a variable
Set ws = Worksheets(i)
‘ Perform your routine here
‘ For example, this adds “Processed” in cell A1 of each sheet
ws.Range(“A1”).Value = “Processed”
Next i
End Sub
“`
### Explanation:
1. **Worksheets Collection**: Each sheet in the workbook is indexed starting from 1, based on its position in the workbook.
2. **Looping from 30 to 40**: The `For` loop iterates through the sheets from the 30th to the 40th.
3. **Routine on Each Sheet**: Inside the loop, you can add your custom routine that needs to be applied to the selected sheets.
### Important Notes:
– Make sure that your workbook contains at least 40 sheets, or you’ll encounter an “index out of range” error.
– If your target sheets have specific names or positions can vary, you may want to use sheet names or another method to identify them dynamically.
Best regards,
Zach