VBA Countif Multiple Criteria

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Trying to figure out how to make the "Str" criteria have multiple options if the value in B1 is "All".

VBA Code:
Dim rngCriteria1 As Range
Dim rngCriteria2 As Range
Dim lastorw As Long
Dim LastCol As Long
Dim Str As String

With Sheet1
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    LastCol = .Cells(1,  Columns.Count).End(xlToLeft).Column
End With

With Sheet6
    If .Range("B1") = "All" Then
        Str = "Apple, Pair, Orange"
    Else: Str = .Range("B1")
    End If
End With

With Sheet6
    Set rngCriteria1 = Sheet1.Range("A2:M" & lastrow)
    Set rngCriteria2 = Sheet1.Range("B2:M" & lastrow)

    .Range("B3") = WorksheetFunction.CountIfs(rngCriteria1, "=A3", rngCriteria2, Str)

    Set rngCriteria1 = Nothing
    Set rngCriteria2 = Nothing
End With
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Perahps
VBA Code:
With Sheet6
    If .Range("B1") = "All" Then
        Str = "*"
    Else: Str = .Range("B1")
    End If
End With
Or use Str = "?*" if you need to ignore any blanks in the count range.
 
Upvote 0
Solution
Something I just noticed that I missed earlier, I'm not sure that "=A3" will work correctly for the first criteria, it might be safer to use Range("A3").Value instead.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

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