PDA

View Full Version : [SOLVED:] Iterate Sheets In Workbook



juan4412
07-13-2015, 01:25 PM
How do you iterate worksheets in a workbook w/o knowing the sheet count? For example, I can use this to get the total worksheet count.

Dim n As Integern = Sheets.Count


But let's say their are 10 sheets in the workbook, n = 10 how would I iterate each worksheet in the workbook?

Aussiebear
07-13-2015, 04:00 PM
This comes from Microsoft as an example


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
' The following line shows how to reference a sheet within the loop _
'by displaying the worksheet name in a dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
End Sub

SamT
07-13-2015, 04:22 PM
Sub simple()
Dim Sht As Worksheet
For each Sht in Sheets 'sheets in THisWorkbook assumed by VBA
MsgBox Sht.Name
Next
End Sub