PC Review


Reply
Thread Tools Rate Thread

Count non-zero values in a Array

 
 
RyanH
Guest
Posts: n/a
 
      5th Aug 2008
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!
--
Cheers,
Ryan
 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      5th Aug 2008
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


"RyanH" <(E-Mail Removed)> wrote in message
news:747D60F8-EBEE-404E-967B-(E-Mail Removed)...
>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!
> --
> Cheers,
> Ryan


 
Reply With Quote
 
RyanH
Guest
Posts: n/a
 
      5th Aug 2008
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" wrote:

> 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
>
>
> "RyanH" <(E-Mail Removed)> wrote in message
> news:747D60F8-EBEE-404E-967B-(E-Mail Removed)...
> >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!
> > --
> > Cheers,
> > Ryan

>
>

 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      5th Aug 2008
Other than what I posted I can't think of any now.

RBS


"RyanH" <(E-Mail Removed)> wrote in message
news:392E06AF-18CF-4091-B11F-(E-Mail Removed)...
> 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" wrote:
>
>> 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
>>
>>
>> "RyanH" <(E-Mail Removed)> wrote in message
>> news:747D60F8-EBEE-404E-967B-(E-Mail Removed)...
>> >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!
>> > --
>> > Cheers,
>> > Ryan

>>
>>


 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      5th Aug 2008
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

RyanH wrote:
> 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!

 
Reply With Quote
 
RyanH
Guest
Posts: n/a
 
      7th Aug 2008
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.


--
Cheers,
Ryan


"Alan Beban" wrote:

> 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
>
> RyanH wrote:
> > 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!

>

 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      8th Aug 2008
RyanH wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Reference an Array to Count Values? Mashuganah Microsoft Excel Misc 6 4th Dec 2009 04:33 PM
Count occurances of equal values in two col. - array within SUMPRO John_J Microsoft Excel Worksheet Functions 5 30th Sep 2008 02:19 PM
Count certain values in numeric array avi Microsoft Excel Programming 4 24th Mar 2008 08:02 AM
Count quanity of values in an Array =?Utf-8?B?U2ltb24gU2hhdw==?= Microsoft Excel Programming 3 12th Jul 2005 03:12 AM
Count how many different text values in an array. =?Utf-8?B?T1ZFUkxPQUQ=?= Microsoft Excel Worksheet Functions 2 14th Apr 2005 04:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:19 AM.