To find the number of sheets in an Excel workbook using VBA, use the ThisWorkbook.Sheets.Count property. This single line of code returns an integer representing the total number of all sheets, including both standard worksheets and chart sheets, within the workbook where the code is stored. If you need to count only the visible sheets or only specific types of worksheets, you will need a short loop to iterate through the sheets collection and increment a counter based on your specific criteria.

What You'll Need

  • Microsoft Excel installed on a Windows or macOS computer.
  • A workbook containing multiple sheets to test the code.
  • The Developer tab enabled in the Excel Ribbon (if not visible, right-click any tab, select Customize the Ribbon, and check the Developer box).
  • Basic familiarity with opening the Visual Basic Editor (VBE).

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard. This shortcut opens the VBE window, which is the environment where all VBA macros are written and managed.
  2. Insert a New Module: In the VBE menu, go to Insert > Module. This creates a blank space where you can write your custom functions and subroutines. Writing code in a module ensures that the macro can be called from anywhere in the workbook.
  3. Create the Subroutine: Type Sub CountAllSheets() at the top of the module. This defines the start of your macro.
  4. Declare a Variable: To store the number of sheets, you need a variable. Type Dim sheetCount As Integer. Using an Integer is sufficient here since it is highly unlikely a workbook will exceed 32,767 sheets.
  5. Assign the Count Value: Use the Sheets collection count property. Type sheetCount = ThisWorkbook.Sheets.Count. The ThisWorkbook object specifically refers to the file containing the code, which prevents errors if you have multiple Excel files open simultaneously.
  6. Display the Result: To see the answer, use a message box. Type MsgBox "The total number of sheets is " & sheetCount.
  7. Close the Subroutine: Type End Sub to finish the code block.
  8. Run the Macro: You can run the code by pressing F5 while the cursor is inside the subroutine, or by returning to Excel, going to the Developer tab, clicking Macros, selecting "CountAllSheets", and clicking Run.

Counting Only Visible Sheets

The standard .Count property includes hidden and very hidden sheets. If your goal is to count only what the user can actually see, you must use a For Each loop. This process involves looking at every sheet one by one and checking its Visible property.

To do this, declare a variable for the counter (e.g., Dim visibleCount As Integer) and a variable for the sheet object (e.g., Dim ws As Worksheet). Set the counter to zero. Then, create a loop: For Each ws In ThisWorkbook.Worksheets. Inside the loop, add an If statement: If ws.Visible = xlSheetVisible Then visibleCount = visibleCount + 1. Finally, close the loop with Next ws and display the visibleCount variable. This method is essential for reports that only care about active, user-facing data tabs.

Counting Specific Sheet Types

Excel distinguishes between Worksheets (the standard grids) and Charts (sheets that contain only a chart). If you use ThisWorkbook.Sheets.Count, you get the sum of both. However, if you only want the number of data grids, you should use ThisWorkbook.Worksheets.Count. While they sound similar, the Worksheets collection ignores chart sheets entirely.

For more advanced filtering, such as counting only sheets that contain the word "Report" in their name, you can modify the loop mentioned previously. Inside the loop, use If ws.Name Like "*Report*" Then to identify specific sheets. This is particularly useful in large corporate workbooks where naming conventions are used to categorize data.

Common Mistakes

  • Confusing Sheets and Worksheets: Using Sheets.Count when you specifically only want the number of grid-based worksheets. This leads to incorrect counts if the workbook contains dedicated chart sheets.
  • Using ActiveWorkbook instead of ThisWorkbook: ActiveWorkbook refers to whatever file is currently in focus. If the user clicks on another open Excel file while the macro is running, the code will count the sheets in the wrong file. ThisWorkbook is the safer choice for internal macros.
  • Ignoring Hidden Sheets: Assuming .Count only counts visible tabs. This often results in a number higher than what the user sees on their screen, leading to confusion or perceived bugs in the software.
  • Variable Overflow: While rare for sheet counts, using a Byte variable instead of an Integer or Long. A Byte can only hold values up to 255; if the workbook has more, the code will crash with an "Overflow" error.

Tips

  • Use Long for Large Workbooks: Although Integer works for most, using Long is a best practice in modern VBA to avoid any possible overflow issues and is generally handled more efficiently by 64-bit systems.
  • Combine with Loops for Auditing: Instead of just counting, use the count to set the limit of a loop that audits every sheet for errors or formatting issues.
  • Dynamic Naming: Use the sheet count to automatically name a new sheet. For example, Sheets.Add.Name = "Data_" & (ThisWorkbook.Sheets.Count) will name the new sheet based on the current total.
  • Option Explicit: Always place Option Explicit at the very top of your module. This forces you to declare all variables, which prevents typos (like writing sheetCont instead of sheetCount) from creating new, empty variables that return a value of zero.

Troubleshooting

If your code does not run or returns the wrong number, start with these checks:

  • Check for Protected Workbooks: If the workbook structure is protected (Review > Protect Workbook), some VBA operations regarding sheet manipulation may fail, though counting usually remains possible.
  • Verify Macro Settings: If the macro won't start, ensure that macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Disable VBA macros with notification".
  • Check File Extension: Ensure the file is saved as an .xlsm (Excel Macro-Enabled Workbook) or .xlsb (Excel Binary Workbook). Standard .xlsx files cannot store VBA code and will strip the macro upon saving.
  • Debug with Immediate Window: If the message box isn't appearing, press Ctrl + G in the VBE to open the Immediate Window. Type ? ThisWorkbook.Sheets.Count and press Enter to see the value instantly without running a full macro.
  • Handle Empty Workbooks: While Excel always has at least one sheet, if your code is designed to delete sheets and then count, ensure you aren't trying to delete the very last sheet, as this will trigger a runtime error.

Frequently Asked Questions

Can I count sheets in a different workbook that is already open?

Yes. Instead of ThisWorkbook, use Workbooks("FileName.xlsx").Sheets.Count, replacing "FileName.xlsx" with the actual name of the open file.

How do I count only the hidden sheets?

Use a loop and check if ws.Visible is equal to xlSheetHidden or xlSheetVeryHidden, incrementing your counter only when these conditions are true.

Is there a limit to how many sheets VBA can count?

VBA can count as many sheets as Excel can support. The limit is primarily determined by your computer's available memory (RAM).

Does this count the "Chart" sheets?

If you use Sheets.Count, yes. If you use Worksheets.Count, no; it only counts the standard spreadsheet tabs.

Can I use this in a cell formula instead of a macro?

Not with a standard formula. You must create a User Defined Function (UDF) in VBA that returns the count, then call that function in your cell like =CountSheets().