VBA计数非空元素的数组 [英] VBA count non empty elements of array
本文介绍了VBA计数非空元素的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的尝试:
Dim Arr(1到15)As Double
'填充Arr
'的一些元素...
Dim nonEmptyElements As Integer,i As Integer
nonEmptyElements = 0:i = 0
对于i = LBound(Arr)到UBound(Arr)
如果不是Arr(i)=然后
nonEmptyElements = nonEmptyElements + 1
结束如果
下一个
有了这个程序,我得到如果尝试将if条件更改为如果不是IsEmpty(Arr(i))Then
nonEmptyElements = 15
。 有关如何完成代码?
解决方案
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
对于i = LBound(Arr)到UBound(Arr)
如果不是Arr(i)= 0然后
nonEmptyElements = nonEmptyElements + 1
结束If
下一个
默认值为double,因此请检查:
Arr(i)= 0
Noob question: I want to count the non empty elements of an array?
My attempt:
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
With this program I get the error: Type mismatch on If statement.
If try to change the if condition to If Not IsEmpty(Arr(i)) Then
and i get nonEmptyElements = 15
as a result.
Any suggestions on how to complete the code?
解决方案
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
A double value by default is 0.0, so check if:
Arr(i) = 0
这篇关于VBA计数非空元素的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文