Excel Count values used in formula

mohibur

New Member
Joined
May 28, 2019
Messages
3
Hi,
if i had cell value for example
=21+22 answer should be 2 as it has used 2 value in the formula
=41+40+41 answer should be 3 as it has used 3 value in the formula

now in adjacent cell i need the count of values in that formula
=21+222
=41+40+413

<tbody>
</tbody>
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hey, do you see the cell as "=21+22" or as 43?

If you see the cell already as "=21+22" then try this:

Code:
Function CountPlus(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\+"
        CountPlus = Len(txt) - Len(.Replace(txt, "")) + 1
    End With
End Function

=CountPlus(A1)

Note: This will only work if all of the numbers have + signs (so if you have division etc it won't work), it requires the text to be already FORMULATEXT formatted, so if you see the cell as 43 (instead of =21+22) then try this:

=CountPlus(FORMULATEXT(A1))

Assuming A1 is where your cell value is.
 
Last edited:
Upvote 0
For the two examples you've shown, try
=LEN(FORMULATEXT(A2))-LEN(SUBSTITUTE(FORMULATEXT(A2),"+",""))+1
 
Upvote 0
OK in that case it is probably easier to define your own UDF that replicates FORMULATEXT

Code:
Function FORMULATEXTS(cell As Range)
    FORMULATEXTS = cell.Formula
End Function

Then you can use this and embed it into any of the solutions in this thread.

E.g. if you use my CountPlus function too then if the text is in cell A1:

=CountPlus(FORMULATEXTS(A1))

Returns 3 if for example A1 is "=41+42+40" which shows up as 123.
 
Last edited:
Upvote 0
You can try with PowerQuery (add-in for Excel 2010)

srcCountPlusEvaluate
21+22
2​
43​
41+40+41
3​
122​

all you need is replace = sign with nothing in src column
then
Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"src"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "Custom", each Table.Column([Count],"src")),
    Count = Table.AddColumn(List, "CountPlus", each List.Count(Text.Split([src],"+"))),
    Eval = Table.AddColumn(Count, "Evaluate", each Expression.Evaluate([src])),
    RC = Table.RemoveColumns(Eval,{"src", "Count", "Custom"})
in
    RC[/SIZE]
 
Upvote 0

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

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