Reputation: 101
Can you help me with a COUNTIF
formula?
I have these conditions - as if I were applying a filter:
Tablet
and NA
<>Duplicate TT
(i.e select everything expect Duplicate TT)Yes
I'm using the below formula but getting the wrong count:
With ActiveWorkbook.Worksheets("TT")
[AH124] = wf.CountIfs(.Range("K:K"), "Tablet", .Range("K:K"), "NA") + wf.CountIf(.Range("I:I"), "<>Duplicate TT") + wf.CountIf(.Range("G:G"), "Yes")
End With
Upvotes: 1
Views: 213
Reputation: 1337
Your Range("I:I"), "<>Duplicate TT"
is going to pick all the blanks all the way to the bottom of the sheet so you should restrict the range to the bottom row: Range("I2:I & Range("I" & Cells.Rows.Count).End(xlUp).Row), "<>Duplicate TT"
(this assumes you're stating to the check on the second row because of headers) and you should probably do that for the rest of your formula too
Upvotes: 0
Reputation: 19319
Your first clause is the issue because no value in column K will be both Tablet
and NA
at the same time:
wf.CountIfs(.Range("K:K"), "Tablet", .Range("K:K"), "NA")
So you need to re-arrange the formula:
With ActiveWorkbook.Worksheets("TT")
[AH124] = wf.CountIfs(.Range("K:K"), "Tablet", .Range("I:I"), "<>Duplicate TT", .Range("G:G"), "Yes") + _
wf.CountIfs(.Range("K:K"), "NA", .Range("I:I"), "<>Duplicate TT", .Range("G:G"), "Yes")
End With
Upvotes: 2
Reputation: 14537
Try this :
With ActiveWorkbook.Worksheets("TT")
[AH124] = wf.CountIfs(.Range("K:K"), "=Tablet", .Range("K:K"), "=NA") + _
wf.CountIf(.Range("I:I"), "<>Duplicate TT") + _
wf.CountIf(.Range("G:G"), "=Yes")
End With
I've added =
in your tests, because without it it'll count all cells containing Tablet, NA, Yes, ...
Upvotes: 0