Count number of specific values in an array (VBA)

selant

Board Regular
Joined
Mar 26, 2009
Messages
109
I want to count how many "woman" values in my person array. Example as follows :
Can anybody share the solution for the function if there is ? Thank you.


Code:
Private Sub CommandButton1_Click()


Dim person(1 To 5) As String


person(1) = "man"
person(2) = "woman"
person(3) = "woman"
person(4) = "man"
person(5) = "man"


Label1.Caption = "There are 2 women" 'calculate how many "woman" in person array
Label2.Caption = "There are 3 men" 'calculate how many "man" in person array


End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Something like

Code:
Sub k1()

Dim person(1 To 5) As String


person(1) = "man"
person(2) = "woman"
person(3) = "woman"
person(4) = "man"
person(5) = "man"

w1 = 0
m1 = 0
For i = 1 To 5
If person(i) = "woman" Then w1 = w1 + 1
If person(i) = "man" Then m1 = m1 + 1
Next i
Debug.Print "There are "; w1; " women"
Debug.Print "There are "; m1; " men"

End Sub
 
Upvote 0
Maybe

Code:
Label1.Caption = "There are " & Application.CountIf(person, "woman") 'calculate how many "woman" in person array
 
Upvote 0
i am getting

Compile Error :
Type Mismatch error


for
Code:
Label1.Caption = "There are " & Application.CountIf(person, "woman") 'calculate how many "woman" in person array
</pre>where is the error ?
 
Upvote 0
The error is that Countif doesn't work with arrays (only ranges). Looping is the way to go (especially with a limited dataset as your example had) as Special-K99 originally showed you,
 
Upvote 0
Code:
Private Sub CommandButton1_Click()
Dim person(1 To 5) As String, mw(0 To 1) As Long, i As Long
person(1) = "man"
person(2) = "woman"
person(3) = "woman"
person(4) = "man"
person(5) = "man"
For i = 1 To 5
    mw(0) = mw(0) - (person(i) = "woman")
    mw(1) = mw(1) - (person(i) = "man")
Next
Label1.Caption = "There are " & mw(0) & " women"
Label2.Caption = "There are " & mw(1) & " men"
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top