Application.CountIf not working the way it seems like it should

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Code:
If Application.WorksheetFunction.CountIf(Range("C1:C27"), Cell) = 0 Then...

This condition does not trigger even when this returns a "0":

Code:
MsgBox Application.WorksheetFunction.CountIf(Range("C1:C27"), Cell)

How do I use CountIf with a condition in code?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try one of these.


Code:
    If Application.WorksheetFunction.CountIf(Range("A1:A5"), "<>" & vbNullString) Then

Code:
    If Application.WorksheetFunction.CountIf(Range("A1:A5"), "<>" & "") Then
 
Upvote 0
Code:
For Each Cell In Range("MyRange1")
 
    If Application.WorksheetFunction.CountIf(Range("MyRange2"), Cell) > 0 Then GoTo theNext
 
    i = i + 1
 
    Range("J" & i) = Cell
 
theNext:
 
Next

This worked exactly the way I thought it should last night. I dunno what was going wrong. Thanks anyway.
 
Upvote 0
What exactly is the code supposed to do? As it stands, it loops through MyRange1, comparing it to MyRange2 (which I assume is a single cell?) and copies out the content of the cell if it doesn't match.

I would rewrite this to avoid the GoTo call.

Code:
    For Each Cell In Range("MyRange1")
 
    If Application.WorksheetFunction.CountIf(Range("MyRange2"), "<>" & Cell) Then
        i = i + 1
        Range("J" & i) = Cell
    End If
    Next
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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