+ Reply to Thread
Results 1 to 7 of 7

Thread: Count elements in Array

  1. #1
    Registered User
    Join Date
    09-01-2009
    Location
    Mexico
    MS-Off Ver
    Excel 2007
    Posts
    25

    Count elements in Array

    Hi there. I was wondering if there is a way to have the elements of an array counted. I have something like this:

    Dim i, array(), arrayTF()
    
    'The array() is already populated with random True or False elements
    
        For i= 1 To 20
            If array(i, 1) = False Then
                arrayTF(i) = False
            Else
                arrayTF(i) = True
            End If
        Next j
    How can I count the numbers of FALSE elements in the arrayTF() array? Your help is always very appreciated.
    Last edited by Rodas; 09-08-2009 at 02:39 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Count elements in Array

    There might be a quicker way, but you could loop through each element and use a counter.

    Why are you moving the values from one array to another?

  3. #3
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Count elements in Array

    Sub x()
        Dim ab(1 To 100) As Boolean
        Dim i As Long
        Dim nFalse As Long
        
        For i = 1 To 100
            ab(i) = Rnd > 0.5
        Next i
        
        For i = 1 To 100
            If Not ab(i) Then nFalse = nFalse + 1
        Next i
            
        MsgBox nFalse
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    09-01-2009
    Location
    Mexico
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Count elements in Array

    It was a quick idea that I had to resolve this (it was intented to re-populate the array for every column) but if there's a better idea, I won't hesitate in taking it. Thanks a lot.

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Count elements in Array

    Have you seen shg's code which does it all very neatly in one?

  6. #6
    Registered User
    Join Date
    09-01-2009
    Location
    Mexico
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Count elements in Array

    This is just what I was looking for, thanks a lot StephenR and shg. By the way (and just for the record), is there any function that does the same but without having to loop through the elements of the array?

  7. #7
    Registered User
    Join Date
    09-01-2009
    Location
    Mexico
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Count elements in Array

    Nevermind, I figured this out already, I just want to thank you guys for your help. Keep it up!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0