vba 如何计算excel表格中一列中填充单元格的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27993141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:17:37  来源:igfitidea点击:

how to count of the number of filled cells in a column in an excel sheet

excelvbaexcel-vbacount

提问by ajs

So I'm currently trying to get a count of the number of filled cells in a column in an excel sheet, but I'm unsure as to how to do that. I've seen things like:

所以我目前正在尝试计算 Excel 工作表中一列中已填充单元格的数量,但我不确定如何做到这一点。我见过这样的事情:

Range("A1").End(xlDown).Row

but that doesn't give an actual count. Does anyone know how to get an integer valued count? Are there any methods built into excel for that purpose?

但这并没有给出实际计数。有谁知道如何获得整数值计数?有没有为此目的内置于excel中的方法?

Thanks in advance.

提前致谢。

回答by bilbo_strikes_back

The counta function could work.

counta 函数可以工作。

'If the data is in column A
Dim lngCount as Long
lngCount = Application.WorksheetFunction.CountA(Columns(1))
msgbox lngCount

回答by Qbik

You can also loop throught the cells range, I wonder which method is faster - this or using Excel's CountA (below would go into almost infinite loop if rrange is the whole column):

您还可以遍历单元格范围,我想知道哪种方法更快 - 这或使用 Excel 的 CountA (如果r范围是整列,下面将进入几乎无限循环):

Sub test()

Dim r As Range
Dim c As Long

Set r = Range("A1").End(xlDown).Row

For Each x In r
   If Not r = "" Then
      c = c + 1
   End If
Next r

MsgBox r

End Sub