Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何验证组中的工作表是否为空或不包含任何数据?_Vba_Excel - Fatal编程技术网

Vba 如何验证组中的工作表是否为空或不包含任何数据?

Vba 如何验证组中的工作表是否为空或不包含任何数据?,vba,excel,Vba,Excel,我想找到一种方法来检查“工作表”组中的任何工作表是否为空或包含数据 我试过用这个,但似乎不起作用 .Sheets(Array("1", "2", "3", "4", "5", "6", "7", "8", "9")).select If WorksheetFunction.CountA(Selection.UsedRange.Cells) = 0 Then MsgBox "All Sheets empty!" Exit Sub End If 由于某些原因,如果我只使用“单元格”,我可

我想找到一种方法来检查“工作表”组中的任何工作表是否为空或包含数据

我试过用这个,但似乎不起作用

.Sheets(Array("1", "2", "3", "4", "5", "6", "7", "8", "9")).select
If WorksheetFunction.CountA(Selection.UsedRange.Cells) = 0 Then 
  MsgBox "All Sheets empty!"
  Exit Sub
End If

由于某些原因,如果我只使用“单元格”,我可以检查工作表是否为空。不知道为什么这样做,因为它没有引用任何分组的图纸。如果其他工作簿也打开,则可能会失败

Sub SheetsEmpty()
With ThisWorkbook
    ' check if sheets are empty.
    .Sheets(Array("1", "2", "3", "4", "5", "6", "7", "8", "9")).Select
    If WorksheetFunction.CountA(Cells) = 0 Then Exit Sub
End With
End Sub
如果我在“监视”窗口中的“WorksheetFunction.CountA(Cells)”中添加一个监视,它只显示一个非空的单元格计数,而不是所有9张工作表的单元格计数。我想这是因为床单是分组的


对这种行为有什么看法吗?

有两种可能性:

如果图纸“1”至“9”相邻,且其间没有其他图纸,则使用:

Sub Demo1()
    With ThisWorkbook
        If Evaluate("=COUNTA('[" & .Name & "]1:9'!1:" & .Worksheets("1").Rows.Count & ")") = 0 Then
            MsgBox "All Sheets empty!"
        Else
            MsgBox "All Sheets NOT empty!"
        End If
    End With
End Sub
如果工作表“1”至“9”不相邻,或中间有其他工作表,请使用:
(或者直接使用此版本)


您需要为工作表数组中的每个工作表循环
工作表函数.CountA(Cells)
不起作用的原因是它对活动工作表上的单元格进行计数(即使选择了mor而不是one),请不要将问题的更新作为答案发布。改为编辑你的问题。谢谢Chris。干杯
Sub Demo2()
    Dim ws As Worksheet
    Dim tot As Long
    For Each ws In ThisWorkbook.Worksheets(Array("1", "2", "3", "4", "5", "6", "7", "8", "9"))
        tot = tot + WorksheetFunction.CountA(ws.Cells)
    Next
    If tot = 0 Then
        MsgBox "All Sheets empty!"
    Else
        MsgBox "All Sheets NOT empty!"
    End If
End Sub