Determine if character appears in SUM function

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
In cell A5, I'd like to return "Yes" if the text within any SUM function in E8 contains a "+" sign.

Should return "Yes" in A5 if E8: =SUM(D2+E2+F2)+SUM(K6:K19)+SUM(X4+Y4+Z4)
Should return "Yes" in A5 if E8: =SUM(D2:F2)+SUM(K6:K19)+SUM(X4+Y4+Z4)
Should return "Yes" in A5 if E8: =SUM(D2+E2+F2)
Should not
return "Yes" in A5 if E8: =SUM(D2:F2)+SUM(K6:K19)+SUM(X4:Z4)
Should not return "Yes" in A5 if E8: =SUM(D2:F2)

The "+" signs between the SUM functions are not relevant to returning a "Yes".

Can this be done with either VBA or a formula? Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Will the cell only ever have a series of SUM()+SUM() functions in it, or are there other possibilities?
Also, do you have functions like TEXTSPLIT?
 
Upvote 0
I can understand @Rorys questions.

This might get you what you want (as a formula for E8), but its by no means bomb proof .. if for example you are setting an exam test or the like.. what I mean is, it could be fooled by a mistake on the user part. So I post it only on the basis you understand that its bit of a hack. But if it helps you, then great.. otherwise Rory is gonna come up with something more bomb proof once you answer his questions :)

Excel Formula:
=IF(ISNUMBER(SEARCH("2+",FORMULATEXT(E8))+(SEARCH("4+",FORMULATEXT(E8)))),"YES","NO")
 
Upvote 0
It is possible there could be other functions, such as IF, in the formula string. SUM is the only one I'm interested in knowing if "+" signs are used.

I don't have TEXTSPLIT, TEXTAFTER, or TEXTBEFORE. I might have been able to work it out if I had those... I have Excel for Microsoft 365 - should be the latest version since I just bought it.

Thanks so much!
 
Upvote 0
I think this UDF would work:

VBA Code:
Function HasSumWithPlus(checkCell As Range)
   Dim formulaText As String
   formulaText = checkCell.Formula
   
   With CreateObject("vbscript.regexp")
      .Pattern = "SUM([^)]*\+[^)]*)"
      .ignorecase = True
      .Global = True
      HasSumWithPlus = .Test(formulaText)
   End With
End Function
 
Upvote 0
Solution
I can understand @Rorys questions.

This might get you what you want (as a formula for E8), but its by no means bomb proof .. if for example you are setting an exam test or the like.. what I mean is, it could be fooled by a mistake on the user part. So I post it only on the basis you understand that its bit of a hack. But if it helps you, then great.. otherwise Rory is gonna come up with something more bomb proof once you answer his questions :)

Excel Formula:
=IF(ISNUMBER(SEARCH("2+",FORMULATEXT(E8))+(SEARCH("4+",FORMULATEXT(E8)))),"YES","NO")
Thanks for the suggestion. I tested it and it seems it is also considering the plus signs between the SUM functions. I only want it to look to see if there are plus signs within the parenthesis of the SUM functions.
 
Upvote 0
I think this UDF would work:

VBA Code:
Function HasSumWithPlus(checkCell As Range)
   Dim formulaText As String
   formulaText = checkCell.Formula
  
   With CreateObject("vbscript.regexp")
      .Pattern = "SUM([^)]*\+[^)]*)"
      .ignorecase = True
      .Global = True
      HasSumWithPlus = .Test(formulaText)
   End With
End Function
YES! Just tested your UDF - does exactly what I need! Thank you so much for all the work in writing this customized code for me! All the best, C
 
Upvote 0
Have a go with:

Excel Formula:
=NOT(ISERROR(FILTERXML("<t><s>"&SUBSTITUTE(FORMULATEXT(A1),"+SUM(","</s><s>SUM(")&"</s></t>","//s[contains(., '+') and starts-with(., 'SUM(')][1]")))

The idea here:

* Split FORMULATEXT() using FILTERXML() by putting xml end/start tags before each occurence of '+SUM(';
* Check each node against: '[contains(., '+') and starts-with(., 'SUM(')]' to see that there is at least an element that starts with 'SUM(' and does contain a '+';
* '[1]' would then return only a single element from whatever elements remain.
 
Last edited:
Upvote 0
Have a go with:

Excel Formula:
=NOT(ISERROR(FILTERXML("<t><s>"&SUBSTITUTE(FORMULATEXT(A1),"+SUM","</s><s>")&"</s></t>","//s[contains(., '+')][1]")))

The idea here is that we split the formulatext using filterxml and use xpath expressions to test against the remaining elements.
Awesome - yes, this does work perfectly. Appreciate your explanation as well. Thanks so much!
 
Upvote 0
Awesome - yes, this does work perfectly. Appreciate your explanation as well. Thanks so much!
Actually, just noted that =SUM(D11+E11)+F11*J$19 is returning false, although there is a plus sign in the SUM function.
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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