QP School

Full Version: Loop through worksheets and print their names in the immediate window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub PrintWorksheetNames()
    Dim ws As Worksheet

    'Loop through each worksheet in the active workbook
    For Each ws In ThisWorkbook.Sheets
        Debug.Print ws.Name
    Next ws
End Sub

Explanation:

The code defines a subroutine named PrintWorksheetNames.
It declares a variable ws of type Worksheet, which will be used to loop through each worksheet in the active workbook.
The For Each loop iterates through each worksheet in the ThisWorkbook.Sheets collection.
The Debug.Print statement is used to print the name of each worksheet in the Immediate window, which can be accessed by pressing Ctrl + G.