Avoid Double counting with COUNTIF

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Book5
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N42ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P43*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF33*N
Sheet1
Cell Formulas
RangeFormula
E2:E4E2=SUM(COUNTIF(A2:D2,$H$2:$H$4))


Trying to get this formula to count relevant cells according to the criteria set in column H. I've bolded the cells that should be counted. My formula appears to work in only one of the 3 ranges ie row 4. I suspect there's double counting going on. Can my formula be tweaked to work right?
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Shouldn't the ABC* be 5 not 2 ??
Book1
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N52ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P33*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF33*N
Sheet1
Cell Formulas
RangeFormula
E2:E4E2=COUNTIF($A$2:$D$4,H2)
 
Upvote 0
Shouldn't the ABC* be 5 not 2 ??
Book1
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N52ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P33*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF33*N
Sheet1
Cell Formulas
RangeFormula
E2:E4E2=COUNTIF($A$2:$D$4,H2)
Each row is a separate record, all 3 criteria apply to each record.
 
Upvote 0
I'm just trying (not an expert), is it like this?
Book1
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N2ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P3*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF3*N
Sheet1
Cell Formulas
RangeFormula
F2:F4F2=SUM(IF(SUM(COUNTIF(A2,{"ABC1*";"*DEF";"*N"}))>0,1,0),IF(SUM(COUNTIF(B2,{"ABC1*";"*DEF";"*N"}))>0,1,0),IF(SUM(COUNTIF(C2,{"ABC1*";"*DEF";"*N"}))>0,1,0),IF(SUM(COUNTIF(D2,{"ABC1*";"*DEF";"*N"}))>0,1,0))
 
Upvote 0
I'm just trying (not an expert), is it like this?
Book1
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N2ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P3*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF3*N
Sheet1
Cell Formulas
RangeFormula
F2:F4F2=SUM(IF(SUM(COUNTIF(A2,{"ABC1*";"*DEF";"*N"}))>0,1,0),IF(SUM(COUNTIF(B2,{"ABC1*";"*DEF";"*N"}))>0,1,0),IF(SUM(COUNTIF(C2,{"ABC1*";"*DEF";"*N"}))>0,1,0),IF(SUM(COUNTIF(D2,{"ABC1*";"*DEF";"*N"}))>0,1,0))
It would be preferable if the criteria wasn't hard-coded because the criteria will change depending on the type of analysis.
 
Upvote 0
test.xlsm
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N22ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P33*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF33*N
Sheet4
Cell Formulas
RangeFormula
E2:E4E2=SUMPRODUCT(--(MMULT({1,1,1},IFERROR(SEARCH($H$2:$H$4,A2:D2),0))>0))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
test.xlsm
ABCDEFGH
1Should returnCriteria
2ABC1000 NABC2000 PABC2100 CABC1020 N22ABC1*
3ABC1000 NABC2000 PABC2100 DEFABC1020 P33*DEF
4ABC1000 DABC2000 PABC2100 DEFABC2020 DEF33*N
Sheet4
Cell Formulas
RangeFormula
E2:E4E2=SUMPRODUCT(--(MMULT({1,1,1},IFERROR(SEARCH($H$2:$H$4,A2:D2),0))>0))
Press CTRL+SHIFT+ENTER to enter array formulas.
Thanks hnds, that works. Haven't used MMULT before but it appears to be an effective function in this case. I guess that COUNTIF was not the most appropriate function to use here.
A follow-up question, what if I wanted to exclude all N records, I tried <>N but it didn't change the count.
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,570
Members
449,318
Latest member
Son Raphon

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