Countif for VBA array without a loop

sheetspread

Well-known Member
Joined
Sep 19, 2005
Messages
5,161
These two posts suggest it can't be done:

http://www.mrexcel.com/forum/excel-questions/672885-visual-basic-applications-countif-array.html

http://www.mrexcel.com/forum/excel-...lications]-count-number-occurances-array.html


But shg said:

You can use worksheet functions in VBA, and could use WorksheetFunction.SumProduct for this. Some functions (CountIf, Rank, some others) work only with ranges (and IntelliSense will tell you), but SumProduct is happy with arrays.

I tried using evaluate sumproduct with a myarray (as variant) and couldn't get it to work.

Example: myarray = array("First","Second","Second","Third","Fourth")

then what would the countif(myarray,"second") syntax be? (the result should equal 2 counts)


The only other method I can think of is printing the array to a worksheet range (which doesn't require a loop), counting off that, then deleting.
 
Try:


Excel 2010
AB
1Seventh
2Second
3Second
4First
5Second
6Second
7Second
8First
9Second
10Second
11Third
12Second
13Second
14Fifth
15Second
Sheet6


Code:
Sub a()
Range("B1").Value = Application.CountIf(Range("a1:a15"), "Second")
End Sub


Excel 2010
AB
1Seventh10
2Second
3Second
4First
5Second
6Second
7Second
8First
9Second
10Second
11Third
12Second
13Second
14Fifth
15Second
Sheet6
 
Last edited:
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,223,405
Messages
6,171,925
Members
452,433
Latest member
Woodchuck76

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