vba Countif formula on Arr

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Need your help , I am trying to use countif formula on array value, but its not working.

MsgBox WorksheetFunction.CountIf(arr, "Completed") Why this line not working.
MsgBox WorksheetFunction.CountA(arr) this line gives result.

Sub Test()

Dim arr As Variant
arr = Range("A2:a30").Value

Range("b5").FormulaR1C1 = "=COUNTIF(R2C1:R30C1,""Completed"")" ' This line works.
MsgBox WorksheetFunction.CountIf(arr, "Completed") not working

End Sub

Thanks
mg
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
CountIf() does not accept arrays. Try pass it a Range object instead...

VBA Code:
Sub Test()

    Dim rng As Range
    Set rng = Range("A2:A30")
   
    Range("b5").FormulaR1C1 = "=COUNTIF(R2C1:R30C1,""Completed"")" ' This line works.
   
    MsgBox WorksheetFunction.CountIf(rng, "Completed")

End Sub

Hope this helps!
 
Upvote 0
Hi Dominic,

Thanks for your help, I have below question.

is there a other way to count specific values from Array.
Can't we apply any formula on Data stored in Array.

Regards,
mg
 
Upvote 0
is there a other way to count specific values from Array.

One way would be to simply loop through each element of the array, and increment a count for each one that meets the criteria. However, here's another way that uses the Filter function to filter the array for the specified criteria, and then uses the CountA method to return a count of elements in the filtered array.

Code:
Sub Test()

    Dim arr As Variant
    arr = Range("A2:A30").Value
    
    Dim filteredArray() As String
    filteredArray = VBA.Filter(Application.Transpose(arr), "Completed", True)
    
    If UBound(filteredArray) <> -1 Then
        MsgBox Application.CountA(filteredArray)
    Else
        MsgBox "No record found!", vbExclamation
    End If

End Sub

Can't we apply any formula on Data stored in Array.

The method will depend on the data type, and what it is that you want to do.
 
Upvote 0
Hi Dominic,

Thanks for your help, it worked !!!

(y)
Regards,
mg
 
Upvote 0
You're very welcome, glad I could help, and thanks for the feedback, cheers!
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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