Noob问题:我想计算一个数组的非空元素?
我的尝试:
Dim Arr(1 To 15) As Double
'populating some of the elements of Arr
'...
Dim nonEmptyElements As Integer, i As Integer
nonEmptyElements = 0: i = 0
For i = LBound(Arr) To UBound(Arr)
If Not Arr(i) = "" Then
nonEmptyElements = nonEmptyElements + 1
End If
Next
使用此程序,我收到错误:在If语句中键入不匹配。
如果尝试将if条件更改为If Not IsEmpty(Arr(i)) Then
,我会得到nonEmptyElements = 15
。
有关如何完成代码的任何建议吗?
答案 0 :(得分:1)
Dim Arr(0 To 15) As Double
Arr(6) = 1.2
Arr(3) = 7
Dim nonEmptyElements As Integer, i As Integer
nonEmptyElements = 0 : i = 0
For i = LBound(Arr) To UBound(Arr)
If Not Arr(i) = 0 Then
nonEmptyElements = nonEmptyElements + 1
End If
Next
默认值为double,因此请检查:
Arr(i) = 0
答案 1 :(得分:1)
Application.CountA(myarray)
CountA是一个用于计算非空值的工作表函数。
仅适用于VBA6,在VBA7中不起作用。