Excel VBA Worksheet Count: How to Count Sheets
In this article
To count worksheets in Excel VBA, use ThisWorkbook.Worksheets.Count (or ActiveWorkbook.Worksheets.Count) and assign it to a variable or display it with a message box. That single line returns the number of worksheet tabs in the workbook, excluding chart sheets and other sheet types. This guide shows the exact syntax, how to count only visible sheets, how to loop through them, and the mistakes that make the count wrong.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook with at least one worksheet, saved as .xlsm if you want to keep the macro.
- Basic familiarity with opening the VBA Editor (Alt+F11 on Windows, Option+F11 on Mac).
Step-by-Step
- Open the VBA Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The editor opens in a separate window. Menu labels can differ slightly by Excel version and platform, but the Project Explorer and Immediate Window are consistent.
- Insert a module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A blank code window appears. Writing code in a standard module keeps it reusable and avoids the sheet-specific code area.
- Write the basic count. Type the following into the module:
Sub CountSheets() Dim wsCount As Long wsCount = ThisWorkbook.Worksheets.Count MsgBox "Worksheets: " & wsCount End SubThisWorkbookrefers to the workbook containing the macro, which is safer thanActiveWorkbookbecause the active workbook changes when the user clicks another file. - Run the macro. Place the cursor inside the Sub and press F5, or use Run > Run Sub/UserForm. A message box shows the count. If you prefer not to see a dialog, replace
MsgBoxwithDebug.Print wsCountand read the result in the Immediate Window (Ctrl+G). - Count only visible worksheets. Hidden sheets are included in
Worksheets.Count. To exclude them, loop and test theVisibleproperty:Sub CountVisibleSheets() Dim ws As Worksheet Dim n As Long For Each ws In ThisWorkbook.Worksheets If ws.Visible = xlSheetVisible Then n = n + 1 Next ws MsgBox "Visible worksheets: " & n End SubxlSheetVisibleis the constant for a normal, unhidden sheet. Very hidden sheets returnxlSheetVeryHiddenand are also excluded by this test. - Count sheets in another open workbook. Replace
ThisWorkbookwithWorkbooks("Book2.xlsx")orActiveWorkbook. If the workbook is closed, open it first withWorkbooks.Open; the Count property only works on open workbooks. - Use the count in a loop. A common pattern is to process every sheet by index:
Indexes run from 1 to Count. Avoid deleting sheets inside a forward loop because the count shrinks and indexes shift; loop backwards withDim i As Long For i = 1 To ThisWorkbook.Worksheets.Count ThisWorkbook.Worksheets(i).Range("A1").Value = "Checked" Next iFor i = Count To 1 Step -1if deletion is needed. - Save the file as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx save discards the module.
Common Mistakes
- Confusing Worksheets with Sheets.
Sheets.Countincludes chart sheets, dialog sheets and macro sheets.Worksheets.Countcounts only worksheet tabs. Pick the one that matches what you actually want to report. - Using ActiveWorkbook when you mean ThisWorkbook. If the user switches windows,
ActiveWorkbookpoints somewhere else and the count is wrong or the code errors. - Assuming hidden sheets are excluded. They are not. Hidden and very hidden worksheets are still in the collection.
- Deleting sheets in a forward loop. The collection re-indexes after each delete, so some sheets are skipped. Loop backwards.
- Forgetting that a workbook must have at least one visible sheet. Excel will not let you hide or delete the last visible worksheet, so a count of zero visible sheets is not a valid state.
Tips
- Store the count in a
Longvariable, notInteger. Integer tops out at 32,767, and while that is far beyond normal workbook sizes, Long is the standard choice and avoids overflow surprises. - Use
Debug.Printduring development instead ofMsgBox; it is faster and does not interrupt execution. - If you need a count for display on a sheet, write it to a cell with
Range("A1").Value = wsCountrather than showing a dialog every time. - Combine the count with a name check to confirm a sheet exists before referencing it: loop the collection and compare
ws.Name. - For a quick one-off count without writing a macro, the sheet tab bar and the sheet navigation arrows give a visual indication, but VBA is the reliable method for automation.
Troubleshooting
- Compile error: Variable not defined. Add
Option Explicitat the top of the module and declare every variable withDim. This catches typos early. - Run-time error 424 (Object required). Usually a misspelled workbook name in
Workbooks("..."). Check the exact file name including extension, or useThisWorkbook. - Count looks too high. You are probably using
Sheets.Countand the workbook contains chart sheets. Switch toWorksheets.Count. - Count looks too low. You may be counting only visible sheets, or the code is pointing at a different workbook. Print
ThisWorkbook.Namealongside the count to confirm which file is being read. - Macro does not appear in the list. The code may be in a sheet module rather than a standard module, or the file was saved as .xlsx. Move the code to a module and save as .xlsm.
- Macros disabled. Check the yellow security bar and enable content, or adjust Trust Center settings for the file location. Only enable macros in files you trust.
Frequently Asked Questions
Does Worksheets.Count include hidden sheets?
Yes. Hidden and very hidden worksheets are included. To count only visible sheets, loop through the collection and test ws.Visible = xlSheetVisible.
What is the difference between Sheets.Count and Worksheets.Count?
Sheets includes every sheet type: worksheets, chart sheets, dialog sheets and macro sheets. Worksheets includes only worksheet tabs. Use Worksheets.Count when you mean normal sheets.
Can I count sheets in a closed workbook?
Not directly. The workbook must be open in Excel for VBA to read its collections. Open it with Workbooks.Open, read the count, then close it if needed.
How do I count sheets across all open workbooks?
Loop through the Workbooks collection and add wb.Worksheets.Count for each entry. Remember that add-ins and hidden personal workbooks may also be open.
Why does my count change after deleting a sheet?
The collection updates immediately. If you delete inside a loop, loop backwards from the current count to 1 so indexes stay valid.
Is there a formula version of this?
No built-in worksheet function returns the sheet count. You can use a defined name with GET.WORKBOOK in legacy macro sheets, but VBA with Worksheets.Count is simpler and supported.