VBA检查Workbook是否有多个工作表

时间:2016-10-13 13:28:13

标签: excel vba excel-vba

我到处寻找答案,但我找不到答案。如何检查工作簿中是否有多个工作表。

3 个答案:

答案 0 :(得分:1)

要获取打开工作簿中的工作表数量,请执行以下操作:

Sub qwerty()

    MsgBox "the number of worksheets in this workbook is: " & ThisWorkbook.Worksheets.Count

End Sub

这将排除图表等。
如果您打开多个工作簿,则类似:

    MsgBox "the number of worksheets in this workbook is: " & wb.Worksheets.Count

您在先前声明中Set wb的位置。

答案 1 :(得分:1)

要从 Personal.xlsb 运行,请尝试使用

Public Sub Count_Sheets()

    Debug.Print "You Have " & Application.Sheets.count & " Sheets " ' Immediate Window
    MsgBox "You Have " & Application.Sheets.count & " Sheets "

End Sub

或使用 ActiveWorkbook.Sheets.count

答案 2 :(得分:1)

这最终最适合我。它在这里结合了多个答案来完成它的工作。

Sub CountSheets()
Dim mainWB  As Workbook
Dim mainWS  As Worksheet
Set mainWB = ActiveWorkbook
Set mainWS = mainWB.Sheets(1)

If mainWB.Sheets.Count > 1 Then MsgBox "There is more than one worksheet in this Excel file."
End Sub