当我使用下面的代码检查我的Excel工作表上是否有任何值时,我试图了解如何跳过工作表的第1行(A)。
Sub IsActiveSheetEmpty()
If WorksheetFunction.CountA(Cells) = 0 Then
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
先谢谢
答案 0 :(得分:3)
尝试以下操作(隐式引用活动工作表,因此您可能希望明确说明工作表名称。)它将使用您正在使用的Excel版本的最后一行和一行:
Sub IsActiveSheetEmpty()
If WorksheetFunction.CountA(Range(Cells(2, "A"), Cells(Cells.Rows.Count, Cells.Columns.Count))) = 0 Then
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
答案 1 :(得分:1)
看起来它应该是一个布尔返回函数而不是sub,但是:
Option explicit
Sub IsActiveSheetEmpty()
Dim ws as worksheet
Set ws = Activesheet
With ws
If application.CountA(.range(.range("B1"),.cells(.rows.count,.columns.count))) = 0 Then
MsgBox .Name & " is empty"
Else
MsgBox .Name & " is not empty"
End If
End with
End Sub
未经测试,写在手机上。它能做你想做的吗?
编辑:您认为是指A列?
答案 2 :(得分:1)
Option Explicit
Sub IsActiveSheetEmpty()
Dim myRange As Range
Set myRange = Range("A2",Cells(2,1).SpecialCells(XlCellType.xlCellTypeLastCell))
If WorksheetFunction.CountA(myRange) = 0 Then
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub
答案 3 :(得分:0)
Sub IsActiveSheetEmpty()
If WorksheetFunction.CountA("A2:A5") = 0 Then 'skips A1
MsgBox ActiveSheet.Name & " is empty"
Else
MsgBox ActiveSheet.Name & " is not empty"
End If
End Sub