数组中的VBA计数值

时间:2014-07-03 20:36:31

标签: arrays vba count

我已阅读有关此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

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

帮助后的EDITED版本

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

2 个答案:

答案 0 :(得分:1)

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

例如

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

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

答案 1 :(得分:0)

你可以试试这个:

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

100%它有效。