Home » excel » excel – Count number of numbers in a single cell

# excel – Count number of numbers in a single cell

Questions:

I want to count the number of numbers in a single cell.

Example:

In cell A1, I have the following addition:

`=12+45+23+51+10` which totals (and shows) `141`.

In cell B1, I would like the see how many numbers have been added together, which means that there should be 5 (12 is a number, 45 another one, etc… and all together, there are 5 numbers in cell A1).

I know it seems to be a ridiculous question, but I scanned all the platforms for this issue and did not find any suitable solution. Tried all the `LEN` and `LEN SUBSTITUTE` alternatives out there, but somehow it does not work.

Thank you upfront for your help. Optimal solution would be a excel formula, alternatively VBA would also work.

Excel 2013 has a new function Formulatext() which returns the, well, formula text. Using that the Len() and Substitute() approach works.

``````=LEN(FORMULATEXT(A1))-LEN(SUBSTITUTE(FORMULATEXT(A1),"+",""))+1
``````

Hit Alt+F11 and Insert->Module with the following VBA:

``````Function NumCount(Rng As Range)
x = Split(Rng.Formula, "+")
NumCount = UBound(x) + 1
End Function
``````

Then you can call `=NumCount(A1)` on any cell to get the number of numbers in the formula for that cell.

Use this VBA:
` Function GetFormula(Cell as Range) as String GetFormula = Cell.Formula End Function`

and then to get the number of numbers…

`=LEN(GetFormula(D99))-LEN(SUBSTITUTE(GetFormula(D99),"+",""))`

on this as my D99 contents…
`=45+46+47+50+100`

The one major drawback here is that I’m assuming everything is + if you have -, *,/ or other operators this gets more challenging. you might be able to use replace for each but you’d always be limited to the 4 basic operators… if someone used exponents or mod, this would be harder.

Also possible in earlier Excel versions without VBA – subject to (i) there is always at least one value in the cells, (ii) the operator is always `+` and (iii) the cells are in a column.

Replace `=` with `'=` in that column, apply the substitution, say:

``````=LEN(A1)-LEN(SUBSTITUTE(A1,"+",""))+1
``````

in a different column and Paste Special, Value the results for that other column. Then apply Text To Columns on the original column with `'` as the delimiter.

*There is no way to do this without using a User Defined Function (UDF) written in Excel VBA. Something like this should work:

``````Public Function numsAdded(cell1 As Range)
Dim formulaString As String
formulaString = cell1.Formula
numsAdded = Len(formulaString) - Len(Replace(formulaString, "+", "")) + 1
End Function
``````

Once you’ve added that to a VBA module, you can use it as a function in any cell in your spreadsheet.

*Edit – Apparently there is a way if you have Excel 2013, as teylyn suggests. If you use 2010 or earlier like me, you’ll need VBA.

``````F16=45+65+76 # Gives 186