Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function CountTheOnes(aArray As Variant) As Long
- Dim x As Long
- Dim OnesCount As Long
- For x = LBound(aArray) To UBound(aArray)
- If aArray(x) = 1 Then
- OnesCount = OnesCount + 1
- End If
- Next
- CountTheOnes = OnesCount
- End Function
- ' and to test it:
- Sub TestIt()
- Dim aArray(1 To 10) As Byte
- Dim x As Long
- ' stuff the array with zeros
- For x = LBound(aArray) To UBound(aArray)
- aArray(x) = 0
- Next
- ' then add a couple of random 1s
- aArray(3) = 1
- aArray(7) = 1
- aArray(9) = 1
- x = CountTheOnes(aArray)
- Debug.Print "Ones:" & vbTab & x
- Debug.Print "Zeros:" & vbTab & UBound(aArray) - x
- End Sub
- x = Application.WorksheetFunction.Sum(aArray)
Add Comment
Please, Sign In to add comment