Count non-zero values in a Array

R

RyanH

I currently have the following code to count all the non-zero values in a
Array. Is there a cleaner way to do this?

Option Explicit

Dim PartRow(20) As Integer
Dim PartQty(20) as Single

Sub TestArray()

Dim i As Integer
Dim CounterRow As Integer
Dim CounterQty As Integer

For i = LBound(PartRow) To UBound(PartRow)
If PartRow(i) <> 0 Then
CounterRow = CounterRow + 1
End If
Next i

For i = LBound(PartQty) To UBound(PartQty)
If PartQty(i) <> 0 Then
CounterQty = CounterQty + 1
End If
Next i

If CounterRow = CounterQty Then
MsgBox "They equal; " & CounterRow
Else
MsgBox "They do NOT equal; " & CounterRow & " " & CounterQty
End If

End Sub

Thanks in Advance!
 
R

RB Smissaert

If your arrays were variant arrays then you could do something like this:

Sub test()

Dim i As Long
Dim arr(0 To 3)

For i = 1 To 3
arr(i) = i
Next i

MsgBox Application.WorksheetFunction.CountA(arr)

End Sub

It would then leave out the empty array elements.
As you have declared as Integer and Single there always will be a zero in
the elements that have not been set, so the above won't work.
What is wrong with the code you got? It looks fine to me.


RBS
 
R

RyanH

There is no error with the code I have and I am ok with using this code. I
just wanted to know if there was a cleaner or more efficient way of rewriting
the code such as using a function or something.
--
Cheers,
Ryan


RB Smissaert said:
If your arrays were variant arrays then you could do something like this:

Sub test()

Dim i As Long
Dim arr(0 To 3)

For i = 1 To 3
arr(i) = i
Next i

MsgBox Application.WorksheetFunction.CountA(arr)

End Sub

It would then leave out the empty array elements.
As you have declared as Integer and Single there always will be a zero in
the elements that have not been set, so the above won't work.
What is wrong with the code you got? It looks fine to me.


RBS
 
R

RB Smissaert

Other than what I posted I can't think of any now.

RBS


RyanH said:
There is no error with the code I have and I am ok with using this code.
I
just wanted to know if there was a cleaner or more efficient way of
rewriting
the code such as using a function or something.
 
A

Alan Beban

Your code below treats blanks (i.e., array elements with no value) as 0,
but array elements that contain blank strings (i.e., array element="")
not as 0. Is that your intent?

It also treats 0,1,2,3,4 and 1,2 3 4 0 as "They equal"; is that also
your intent?

Alan Beban
 
R

RyanH

I am curious if there is a more efficient way of counting the number of
non-zero values in each of my arrays. I currently loop through them as you
can see below. I didn't know if there was a function of some typr of
something like that.
 
A

Alan Beban

RyanH said:
I am curious if there is a more efficient way of counting the number of
non-zero values in each of my arrays. I currently loop through them as you
can see below. I didn't know if there was a function of some typr of
something like that.
To suggest a reply I need answers to the questions I posted to you
previously:

"Your code . . . treats blanks (i.e., array elements with no value) as
0, but array elements that contain blank strings (i.e., array
element="") not as 0. Is that your intent?

It also treats 0,1,2,3,4 and 1,2 3 4 0 as "They equal"; is that also
your intent?"

Alan Beban
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top