PDA

View Full Version : Loop Only Through Visible Worksheets



agnesz
09-11-2008, 12:55 PM
I am currently using the following code to loop through all worksheets, however I'd like to adjust it to only loop through the visible ones.
Any thoughts?
Sub WorksheetLoop()

Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
ActiveWorkbook.Worksheets(I).Activate

looppoop
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.

Next I
End Sub

Simon Lloyd
09-11-2008, 01:38 PM
Try this:

Sub WorksheetLoop()

Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
If Worksheets(I).Visible = True Then
ActiveWorkbook.Worksheets(I).Activate

looppoop
End If
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.

Next I
End Sub