VBA-使用单元格引用计数非空白单元格
在VBA中,使用Excel 2016,我试图计算给定范围内非空白单元格的数量,但仅使用单元格整数引用。我尝试了以下方法:VBA-使用单元格引用计数非空白单元格,vba,excel,Vba,Excel,在VBA中,使用Excel 2016,我试图计算给定范围内非空白单元格的数量,但仅使用单元格整数引用。我尝试了以下方法: WB.Sheets(1).Range(Cells(2, X), _ Cells(2, Y)).Cells.SpecialCells(xlCellTypeConstants).count 其中X和Y是列的单元格引用。请协助。尝试以下方法: Dim nonBlanck as long nonBlanck = WorksheetFunction.CountA(WB.She
WB.Sheets(1).Range(Cells(2, X), _
Cells(2, Y)).Cells.SpecialCells(xlCellTypeConstants).count
其中X和Y是列的单元格引用。请协助。尝试以下方法:
Dim nonBlanck as long
nonBlanck = WorksheetFunction.CountA(WB.Sheets(1). _
Range(Cells(2, X), Cells(2, Y)). _
SpecialCells(xlCellTypeConstants))
但是,如果WB.SHeets(1)
未激活,则可能会出现一些问题。因此,请尝试使用此改进的解决方案:
Dim nonBlanck as long
With WB.Sheets(1)
nonBlanck = WorksheetFunction.CountA( _
.Range(.Cells(2, X), .Cells(2, Y)). _
SpecialCells(xlCellTypeConstants))
End With
您可能知道,可以使用excel公式
CountA
执行此操作
您还可以在VBA中使用以下命令:
with WB.sheets(1)
WorksheetFunction.CountA(Range(.Cells(2, X), .Cells(2, Y)))
end with
您需要一种方法来定义剪裁列的范围,然后应用
SpecialCells
:
Sub dural()
Dim x As Long, y As Long, rng As Range
x = 3
y = 5
Set rng = Range(Cells(2, x), Cells(Rows.Count, y))
MsgBox rng.Cells.SpecialCells(xlCellTypeConstants).Count
End Sub
试试这个:
Range(WB.Sheets(1).Cells(2, X), _
WB.Sheets(1).Cells(2, Y)).Cells.SpecialCells(xlCellTypeConstants).count
应将工作簿和工作表添加到单元格之前,而不是区域之前