Adding the same header, updating the same cell value, or applying the same print settings to multiple sheets… There are many situations in Excel where you want to perform the exact same operation on several sheets.
Using VBA, you can execute a loop command on “all currently selected sheets (Work Group)” at once.
This article explains the basic pattern for applying a process to selected multiple sheets using the ActiveWindow.SelectedSheets collection.
The Core Concept: ActiveWindow.SelectedSheets
To get the collection of currently selected sheets in VBA, use the special collection ActiveWindow.SelectedSheets.
- ActiveWindow: Refers to the currently active Excel window.
- SelectedSheets: Returns the collection of sheets currently selected (grouped) within that window.
By running a For Each loop on this SelectedSheets collection, you can process the sheets in the group one by one.
Complete VBA Code
Below is the VBA code that selects specific sheets and then enters the date and formats cell A1 for all of them.
Sub ProcessAllSelectedSheets()
' Declare variables
Dim targetSheet As Worksheet
' --- 1. Select multiple sheets to process (Create a Work Group) ---
' Here, we select specific sheets by name as an example.
' This macro also works if the user manually selects multiple sheets before running it.
On Error Resume Next ' Avoid error if specified sheet names do not exist
ThisWorkbook.Worksheets(Array("Report_Jan", "Report_Feb", "Report_Mar")).Select
If Err.Number <> 0 Then
MsgBox "Some of the specified sheets were not found.", vbCritical
On Error GoTo 0
Exit Sub
End If
On Error GoTo 0
'--- Stop processing if only one sheet (or fewer) is selected ---
If ActiveWindow.SelectedSheets.Count <= 1 Then
MsgBox "Please run this macro with multiple sheets selected.", vbInformation
Exit Sub
End If
' --- 2. Loop through all selected sheets ---
For Each targetSheet In ActiveWindow.SelectedSheets
' --- 3. Execute the same process for each sheet ---
' Example: Enter "Last Updated: (Date)" in cell A1 and make it bold
With targetSheet.Range("A1")
.Value = "Last Updated: " & Date
.Font.Bold = True
End With
Next targetSheet
' --- 4. Ungroup sheets after processing (Recommended) ---
' To prevent accidental input into multiple sheets, re-select just the first sheet.
ThisWorkbook.Worksheets(1).Select
MsgBox "Processing complete for selected sheets."
End Sub
Key Points of the Code
1. Creating a Work Group
ThisWorkbook.Worksheets(Array("Report_Jan", "Report_Feb", "Report_Mar")).Select
First, select the target sheets as a work group. You can omit this part if you want the user to manually select sheets (by holding the Ctrl key) before running the macro.
2. Looping Through Selected Sheets
For Each targetSheet In ActiveWindow.SelectedSheets
This is the heart of the technique. Use a For Each loop to iterate through the sheets contained in the ActiveWindow.SelectedSheets collection, assigning them one by one to the targetSheet variable.
3. Common Processing for Each Sheet
With targetSheet.Range("A1")
.Value = "Last Updated: " & Date
.Font.Bold = True
End With
Inside the loop, the variable targetSheet points to the selected sheets in order. Therefore, operations performed on this variable (like inputting into cell A1 and formatting) are executed on all selected sheets.
4. Ungrouping Sheets (Recommended)
ThisWorkbook.Worksheets(1).Select
If the sheets remain grouped after the macro finishes, the user might accidentally input data into multiple sheets. It is a good design practice to select a single sheet at the end of the process to ungroup them.
Summary
The procedure for performing the same operation on multiple selected sheets is very simple:
- Select the target sheets as a work group (manually or via VBA).
- Start a loop with the syntax
For Each mySheet In ActiveWindow.SelectedSheets. - Write the common processing for each sheet using the variable
mySheetinside the loop. - (Recommended) Re-select a single sheet at the end to ungroup.
This pattern is a powerful technique that is extremely effective when formatting many sheets or updating common items all at once.
