Guest User

Untitled

a guest
Jan 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. Function CountTheOnes(aArray As Variant) As Long
  2. Dim x As Long
  3. Dim OnesCount As Long
  4. For x = LBound(aArray) To UBound(aArray)
  5. If aArray(x) = 1 Then
  6. OnesCount = OnesCount + 1
  7. End If
  8. Next
  9. CountTheOnes = OnesCount
  10. End Function
  11.  
  12. ' and to test it:
  13.  
  14. Sub TestIt()
  15. Dim aArray(1 To 10) As Byte
  16. Dim x As Long
  17.  
  18. ' stuff the array with zeros
  19. For x = LBound(aArray) To UBound(aArray)
  20. aArray(x) = 0
  21. Next
  22.  
  23. ' then add a couple of random 1s
  24. aArray(3) = 1
  25. aArray(7) = 1
  26. aArray(9) = 1
  27.  
  28.  
  29. x = CountTheOnes(aArray)
  30.  
  31. Debug.Print "Ones:" & vbTab & x
  32. Debug.Print "Zeros:" & vbTab & UBound(aArray) - x
  33.  
  34. End Sub
  35.  
  36. x = Application.WorksheetFunction.Sum(aArray)
Add Comment
Please, Sign In to add comment