Use a For Each loop over the Worksheets collection. This macro enters “Yes” in cell A1 of every worksheet in the file that holds the code:
Sub LoopAllSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1").Value = "Yes"
Next ws
End Sub
Use ThisWorkbook for the file containing the macro and ActiveWorkbook for whichever file is on screen. For a closed file, open it with Set wb = Workbooks.Open("C:\Reports\Sales.xlsx"), loop wb.Worksheets, then close it with wb.Close SaveChanges:=True.
To skip a sheet, test its name inside the loop: If ws.Name <> "Summary" Then. Worksheets leaves out chart sheets. If you loop Sheets instead, declare the variable As Object, because a chart sheet in a variable declared As Worksheet stops the macro with a type mismatch error.
For a counter-based loop, use For i = 1 To ThisWorkbook.Worksheets.Count and refer to each sheet as ThisWorkbook.Worksheets(i). If the loop deletes sheets, run it backwards with Step -1. Counting upwards skips the sheet that moves into the deleted one’s place.
The common silent fault is writing Range("A1") inside the loop without the ws. prefix. An unqualified range points at the active sheet, so the macro writes to the same sheet once per loop and leaves the rest untouched.
There are two ways to loop through all the sheets that you have in a workbook.
- FOR NEXT LOOP
- FOR EACH LOOP
1. Using For Each Loop
As you know, with FOR EACH, you can loop through all the objects in a collection, and in a workbook, Worksheets is the collection of all the worksheets.
Use the following steps:
- First, declare a variable to refer to a worksheet for the loop.

- After that, start the loop with the keyword “For Each” and refer to each worksheet in the workbook.

- Now, let’s say you want to enter a value in cell A1 of each worksheet; you can write code like the following.

- In the end, use the keyword “End” to end the loop.

Sub vba_loop_sheets() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets ws.Range("A1").Value = "Yes" Next ws End Sub
This code loops through each sheet and enters the value in cell A1 of each sheet. The benefit of using this method is that it loops through all the sheets in the workbook, without you needing to count them.
And if you want to loop through all the worksheets of a closed workbook, use code like the one below. It opens the workbook, enters the value on each sheet, and then saves and closes it.
Sub vba_loop_sheets() Dim wb As Workbook Dim ws As Worksheet Application.DisplayAlerts = False Set wb = Workbooks.Open("C:\Users\Dell\Desktop\sample-file.xlsx") For Each ws In wb.Worksheets ws.Range("A1").Value = "Done" Next ws wb.Close SaveChanges:=True Application.DisplayAlerts = True End Sub
2. Use the For Next Loop
You can also loop by using the FOR NEXT loop, where you can use the count of the sheets to decide the number of loops to perform and use the loop counter to refer to each sheet.
Here are the steps you need to follow:
- First, you need to declare two variables: one to store the counter value for the loop and one for the count of the sheets that you have in the workbook.

- Now, set the value for the “shtCount” variable equivalent to the number of sheets that you have in the workbook.

- After that, start the loop using the “For i” keyword and use the sheet count as the max value for the loop counter.

- From here, you need to use the loop counter to loop through all the sheets and enter the value “Yes” in cell A1 of every sheet.

Full Code
Sub vba_loop_sheets() Dim i As Long Dim shtCount As Long shtCount = Sheets.Count For i = 1 To shtCount Sheets(i).Range("A1").Value = "Yes" Next i End Sub
And if you want to loop through a workbook that is closed, then use the following code.
Sub vba_loop_sheets() Dim i As Long Dim shtCount As Long Dim wb As Workbook Set wb = Workbooks.Open("C:\Users\Dell\Desktop\sample-file.xlsx") shtCount = wb.Sheets.Count Application.DisplayAlerts = False For i = 1 To shtCount wb.Sheets(i).Range("A1").Value = "Yes" Next i wb.Close SaveChanges:=True Application.DisplayAlerts = True End Sub
Note that Sheets includes chart sheets, while Worksheets has only the worksheets. A chart sheet has no cells, so Sheets(i).Range(“A1”) gives an error on it. If your workbook has chart sheets, use Worksheets.Count and Worksheets(i) instead.
Skip a Sheet in the Loop
Often, you want to run the code on all the sheets except one, like a summary sheet. Use an IF statement with the sheet name inside the loop:
Sub loop_sheets_skip_summary()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
ws.Range("A1").Value = "Yes"
End If
Next ws
End Sub
List All Sheet Names with a Loop
The following code loops through the sheets and writes each sheet’s name in column A of the active sheet, one below the other:
Sub list_sheet_names()
Dim i As Long
For i = 1 To ThisWorkbook.Sheets.Count
ActiveSheet.Cells(i, 1).Value = ThisWorkbook.Sheets(i).Name
Next i
End Sub
Frequently Asked Questions
How do I delete sheets in a loop?
Loop backward with For i = Sheets.Count To 2 Step -1, so deleting a sheet doesn’t change the number of the sheets you haven’t checked yet. A workbook must keep at least one visible sheet, and turn off alerts to skip the confirmation for each sheet.
Yes. Both loops go through hidden and very hidden sheets too. To skip them, check If ws.Visible = xlSheetVisible Then inside the loop.
Which loop is better, For Each or For Next?
For Each is simpler when you just need every sheet. For Next is better when you need the sheet’s position, want to go backward, or want to start from a specific sheet.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.