30 Advanced Excel Tips for 2026

Excel Champs / VBA / How to COUNT Sheets using VBA in Excel
By Puneet Gogia (Microsoft MVP)

How to COUNT Sheets using VBA in Excel

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Read the Count property of the sheets collection:

MsgBox ThisWorkbook.Worksheets.Count

Worksheets.Count counts worksheets only; Sheets.Count adds chart sheets. Both include hidden sheets. Use ThisWorkbook for the file holding the macro and ActiveWorkbook for the file on screen.

For another open file, name it with its extension: Workbooks("Sales.xlsx").Worksheets.Count. If the file is not open, you get “Subscript out of range”.

To count only visible sheets, loop and test each one:

Dim ws As Worksheet, n As Long
For Each ws In ThisWorkbook.Worksheets
    If ws.Visible = xlSheetVisible Then n = n + 1
Next ws
MsgBox n

Without VBA, =SHEETS() returns the number of sheets in the workbook (Excel 2013 and later).

For a closed file, open it read-only and close it without saving, so counting does not change its modified date: Set wb = Workbooks.Open("C:\Reports\Sales.xlsx", ReadOnly:=True), read wb.Worksheets.Count, then wb.Close SaveChanges:=False.

In Excel, if you have many sheets, you can use a VBA code to count them quickly instead of manually counting or using any formula. So, in this post, we will see different ways to count sheets in a workbook.

Count Sheets from the Active Workbook

Following is the code that you need to use to count the sheets in a workbook.

count-sheets-form-the-active-workbook
ThisWorkbook.Sheets.Count

In this code, first, you have referred to the workbook using “ThisWorkbook”, then to all the sheets, and in the end, used the Count property to count all the sheets. Note that ThisWorkbook is the workbook where the code is saved, which isn’t always the active one. To count the sheets of the workbook that is active right now, use ActiveWorkbook:

Sub count_sheets_active_workbook()
MsgBox ActiveWorkbook.Sheets.Count
End Sub

Sheets counts all types of sheets, including chart sheets. And if you want to count only the worksheets instead of all the sheets, then use the following code.

ThisWorkbook.Worksheets.Count

Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook

Count Sheets from a Different Workbook

You can use the name of the workbook to refer to it and then count the sheets in it. Let’s say you want to count the sheets in the workbook “sample-file.xlsx”.

Here’s the code.

Workbooks("sample-file.xlsx").Sheets.Count

This code gives you the count of the sheets that you have in the workbook “sample-file.xlsx”. There is one thing that you need to take care of: this workbook needs to be open.

Count Sheets from All the Open Workbooks

You might have more than one workbook that is open at the same time, and you can count all the sheets from all those workbooks.

Sub vba_loop_all_sheets()
Dim wb As Workbook
Dim i As Long
For Each wb In Application.Workbooks
  If wb.Name <> "PERSONAL.XLSB" Then
      i = i + wb.Sheets.Count
  End If
Next wb
MsgBox "Total sheets in all the open workbooks: " & i
End Sub

Count Sheets from a Closed Workbook

Now here we have a code that refers to the workbook that is saved on my system’s desktop. When I run this code, it opens that workbook, counts the sheets in it, and then adds that count to cell A1 of the first sheet of the workbook with the code.

Sub vba_count_sheets()
Dim wb As Workboo
Application.DisplayAlerts = False
Set wb = Workbooks.Open("C:UsersDellDesktopsample-file.xlsx")
ThisWorkbook.Sheets(1).Range("A1").Value = wb.Sheets.Count
wb.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub

We have turned OFF the display alerts so that Excel doesn’t show any messages while it opens and closes the file.

There are a few problems in the code above: the variable type is cut short (“Workboo” instead of “Workbook”), so the code doesn’t run; the file path has lost its backslashes; and the file is saved when it closes even though nothing in it has changed. Here’s the corrected code, which also turns off screen updating so the file opens in the background:

Sub vba_count_sheets_closed()
Dim wb As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Workbooks.Open("C:\Users\Dell\Desktop\sample-file.xlsx")
ThisWorkbook.Sheets(1).Range("A1").Value = wb.Sheets.Count
wb.Close SaveChanges:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Count Visible and Hidden Sheets

Sheets.Count includes hidden and very hidden sheets as well. To count only the sheets that you can see, loop through the sheets and check the Visible property:

Sub count_visible_sheets()
Dim sh As Object
Dim visibleCount As Long
For Each sh In ActiveWorkbook.Sheets
If sh.Visible = xlSheetVisible Then visibleCount = visibleCount + 1
Next sh
MsgBox "Visible: " & visibleCount & ", Hidden: " & ActiveWorkbook.Sheets.Count - visibleCount
End Sub

Count Sheets with a Formula

If you don’t want to use VBA, Excel 2013 and later versions (including Microsoft 365) have the SHEETS function. Without any argument, it returns the number of sheets in the workbook:

=SHEETS()

SHEETS also counts hidden sheets. And to get the position number of the sheet the formula is on, use the SHEET function instead.

Frequently Asked Questions

What is the difference between Sheets.Count and Worksheets.Count?

Sheets.Count counts all the sheets, including chart sheets. Worksheets.Count counts only the worksheets. If your workbook has no chart sheets, both give the same number.

Why does my count include sheets I cannot see?

Because Count includes hidden sheets. Use the visible sheets code above to count only the sheets that are visible.

How do I count sheets with a specific word in the name?

Loop through the sheets and add 1 to a counter when InStr(1, sh.Name, “Sales”, vbTextCompare) is more than 0, just like the visible sheets code above.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel