vba Countif formula on Arr

Mallesh23

Board Regular
Joined
Feb 4, 2009
Messages
232
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
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,012
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!
 

Mallesh23

Board Regular
Joined
Feb 4, 2009
Messages
232
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
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,012
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.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,012
You're very welcome, glad I could help, and thanks for the feedback, cheers!
 

Forum statistics

Threads
1,082,233
Messages
5,363,877
Members
400,767
Latest member
hnasrat

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top