数组中的 VBA 计数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24562894/
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-12 03:41:32  来源:igfitidea点击:

VBA Count Values in Array

arraysexcelvbacount

提问by Greg

I've read the post on this VBA problem, but my VBA script is still not working.

我已阅读有关此 VBA 问题的帖子,但我的 VBA 脚本仍然无法正常工作。

Public Sub Test()

Dim arrNames As Variant                     'Declare array named "arrNames"
arrNames = Sheet1.Range("F2:F1000")         'Array filled with column F
intN = Application.CountIf(arrNames, "*")   'does not work intent: count cells w/info
'intN = Application.CountA(arrNames)        'works but MsgBox displays value of 999
MsgBox (intN)

End Sub

How do I get the number of cells in my array containing any value?

如何获取数组中包含任何值的单元格数量?

EDITED version after help

帮助后的编辑版本

Public Sub Test()

Dim arrNames As Variant                       'Declare array named "arrNames"
Dim i As Long

arrNames = Sheet1.Range("F2:F1000")           'Array filled with column F

For i = LBound(arrNames) To UBound(arrNames)
    If (arrNames(i,1) = "") Then
        EmptyCounter = EmptyCounter + 1
    End If
Next i

End Sub

采纳答案by crazylane

You can try this:

你可以试试这个:

intN = Worksheets("Sheet1").Range("F2:F1000").Cells.SpecialCells(xlCellTypeConstants).Count
MsgBox intN

100% it works.

100% 有效。

回答by hnk

There is no direct way to do it, as far as I understand. But you could run a simple loop to check if the values are equal to "" assuming string data.

据我所知,没有直接的方法可以做到这一点。但是您可以运行一个简单的循环来检查值是否等于 "" 假设字符串数据。

For e.g.

例如

For i = LBound(ArrayName) to UBound(ArrayName)
    If (ArrayName(i) = "") Then
        EmptyCounter = EmptyCounter + 1
    End If
Next i

If it's numeric or other type of data, you may try variations of the above loop using functions such as IsEmpty(VariableName)etc.

如果它是数字或其他类型的数据,您可以使用诸如IsEmpty(VariableName)等函数尝试上述循环的变体。