Problem using SUBSTITUTE to create CRITERIA for SUMIFS range/criteria pair

Bcarey

New Member
Joined
Jun 9, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I am trying to use SUBSTITUTE to create a criteria for one of range/Criteria of a SUMIFS function.
The function I am trying to get to work is a SUMIIF with 4 range/criteria pairs
  • 1 - Is the row date is equal or greater than the C$1 date value
  • 2 - Is the row date is equal or less than the C$1 date value + 6 (Range/Criteria sets one and two are to insure only including data from one week)
  • 3 - Is the row animal type equal to the $A2 value (only include data for the animal type indicated in column A for this row)
  • 4 - Is the row color match one of the values in $B2 data (only include data for the color types indicated in column B for this row)
The function has no errors but does not match anything and returns a zero.

The SUBSTITUE is taking the cell with the color type and converting it to an OR structure for the forth criteria.
For example cell B2 “Brown, Grey” would become after the nested SUBSTITUTE
{“*Brown*”,”*Grey*”}



If I remove the nested SUBSTITUTE and put {“*Brown*”,”*Grey*”} in directly it works.



**What do I need to do to get the SUMIFS to recognize the output from my nested SUBSTITUTE



NOTE: the substitute is adding the open and close brackets removing the spaces and adding the asterisks and double quotes.



Below is the full function and the spread sheets I am trying to get to work:

=SUM(
SUMIFS(
Sheet2!$D$2:$D$24,
Sheet2!$A$2:$A$24,">="&C$1,
Sheet2!$A$2:$A$24,"<="&C$1+6,
Sheet2!$B$2:$B$24,"="&$A2,
Sheet2!$C$2:$C$24,SUBSTITUTE(
SUBSTITUTE(
"{""*"&$B2&"*""}",
",",
"*"", ""*"
),
" ",
""
)
)
)




Here are the example Sheets I put together that demonstrates this issue

Sheet1:

AnimalColor Type
3/7/22​
3/14/22​
3/21/22​
DOGBrown, Grey
0​
0​
0​
CatMix, Black
0​
0​
0​
HorseBlack, White, Brown
0​
0​
0​
BirdGrey, White
0​
0​
0​


Sheet2:

Dateanimalcolor typecount
3/7/2022​
DogBrown
3​
3/7/2022​
DogWhite
4​
3/7/2022​
DogBlack
3​
3/7/2022​
DogGrey
4​
3/7/2022​
CatMix
8​
3/7/2022​
HorseBrown
5​
3/7/2022​
HorseWhite
4​
3/7/2022​
BirdBlack
7​
3/7/2022​
BirdGrey
9​
3/14/2022​
BirdMix
8​
3/14/2022​
Birdwhite
6​
3/14/2022​
DogWhite
7​
3/14/2022​
DogGrey
2​
3/14/2022​
CatWhite
3​
3/14/2022​
HorseBrown
3​
3/14/2022​
HorseBlack
8​
3/21/2022​
DogGrey
3​
3/21/2022​
DogMix
5​
3/21/2022​
CatBrown
5​
3/21/2022​
CatWhite
9​
3/21/2022​
CatBlack
2​
3/21/2022​
CatGrey
6​
3/21/2022​
HorseMix
2​
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The function was suppose to look like the following, posted it by accident before finishing:
=SUM(
SUMIFS(
Sheet2!$D$2:$D$24,
Sheet2!$A$2:$A$24,">="&C$1,
Sheet2!$A$2:$A$24,"<="&C$1+6,
Sheet2!$B$2:$B$24,"="&$A2,
Sheet2!$C$2:$C$24,SUBSTITUTE(
SUBSTITUTE(
"{""*"&$B2&"*""}",
",",
"*"", ""*"
),
" ",
""
)
)
)
 
Upvote 0
You cannot do that, as the substitute will return a string & not an array, you could use filterxml instead.
Excel Formula:
=SUM(SUMIFS(Sheet2!$D$2:$D$24,Sheet2!$A$2:$A$24,">="&C$1,Sheet2!$A$2:$A$24,"<="&C$1+6,Sheet2!$B$2:$B$24,"="&$A2,Sheet2!$C$2:$C$24,FILTERXML("<k><m>"&SUBSTITUTE($B2,",","</m><m>")&"</m></k>","//m")))
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
How would I use the FILTERXML is I was looking to for cells that did not contain the colors in sheet 1.
For example looking for dogs that are not "Brown, Grey"

I tried the FILTERXML with a "<>" and it added the cells twice once to Brown not in the cell and once for Grey not in the cell,
and I only once when both are not in the cell.
 
Upvote 0
You can't that construct is for OR criteria, whereas you need to use AND.
You would need to use something like
Excel Formula:
=SUM(SUMIFS(Sheet2!$D$2:$D$24,Sheet2!$A$2:$A$24,">="&C$1,Sheet2!$A$2:$A$24,"<="&C$1+6,Sheet2!$B$2:$B$24,$A2,Sheet2!$C$2:$C$24,"<>"&LEFT(B2,FIND(",",B2)-1),Sheet2!$C$2:$C$24,"<>"&MID(B2,FIND(",",B2)+2,100)))
 
Upvote 0
that would be fine if I had a set number, but in my actual sheet I can have from 1 to 5 items to compare at any time
 
Upvote 0
In that cas as it's a totally different question, you will need to start a new thread if you need further help.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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