VBA Count visible sheets (or hidden sheets)

Derek

Well-known Member
Joined
Feb 16, 2002
Messages
1,595
Hi Guys

Need to count how many sheets are visible and how many are hidden.

Have tried - Thisworkbook.sheets.count - but that give them all
Have tried - Thisworkbook.sheets.visible.count - but Excel 2010 doesn't seem to like that.

Please can someone give me the code for this

Many thanks

Derek
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
for each sheet in thisworkbook.sheets

if sheet.visible = true then i = i + 1

next

msgbox i
 
Upvote 0
try:

Code:
Sub vhCount()
    Dim v
    For Each s In ActiveWorkbook.Sheets
        If s.Visible Then v = v + 1
    Next s
    MsgBox "Visible : " & v & vbCrLf & _
        "Hidden : " & ActiveWorkbook.Sheets.Count - v
End Sub

PS, re Glory's code, I'd recommend avoiding variable names that could be mistaken for or even are reserved words, like 'sheet'
 
Upvote 0
I added some improvements to the piece of code from above, to take into account all type of sheets (also those in xlVeryHidden status):
--------------------------------
Sub SheetCounting()
Dim v, h, vh, t As Integer
'count the total number of sheets:
t = ActiveWorkbook.Sheets.Count
'count the number of sheets in status "xlVeryHidden"
vh = 0
For Each s In ActiveWorkbook.Sheets
If (s.Visible = xlVeryHidden) Then vh = vh + 1
Next s
'count the number of sheets in status "xlHidden"
h = 0
For Each s In ActiveWorkbook.Sheets
If (s.Visible = xlHidden) Then h = h + 1
Next s
'count the number of sheets in status "xlVisible"
v = t - h - vh
msgbox "xlVisible = " & v & vbCrLf & "xlHidden = " & h & vbCrLf & "xlVeryHidden = " & vh & vbCrLf & "xlTotal = " & t
End Sub
 
Upvote 0
additional, even simpler way:
-------------------------------

Function SheetCount(intCommand As Integer) 'possible values for the command string: xlSheetHidden, xlSheetVeryHidden, xlSheetVisible or 9
Dim v As Integer
'count the total number of sheets:
If intCommand = 9 Then
SheetCount = ActiveWorkbook.Sheets.Count
Else
v = 0
For Each s In ActiveWorkbook.Sheets
If (s.Visible = intCommand) Then v = v + 1
Next s
SheetCount = v
End If
End Function

Sub Test_SheetCount()
msgbox "visible pages = " & SheetCount(xlSheetVisible)
msgbox "hidden pages = " & SheetCount(xlSheetHidden)
msgbox "very hidden pages = " & SheetCount(xlSheetVeryHidden)
msgbox "total pages = " & SheetCount(9)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top