Sum of digits in a number

B

Bob Ptacek

Example: a number 1234 would sum individual dighits to a value of 10.

I thought there would be a function in Excel that would do that but can't
seem to find it. I don't want write a macro with MID functions to do the
summing. Is there such a function in Excel?

Thank you in advance for any help
 
C

carlo

Hi Bob

there is no direct function, but you could use the following:
=SUMPRODUCT(MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1)*1)

hth

Carlo
 
G

Gord Dibben

Bob

=SUMPRODUCT(--MID($A$1,ROW(INDIRECT("1:" & LEN($A$1))),1))

I forget who originally posted this, but well done!


Gord Dibben MS Excel MVP
 
G

George Nicholson

Is there such a function in Excel?
Afaik, no. but simple enough to create.

Public Function StringSum(str As String) As Long
Dim i As Integer
For i = 1 To Len(str)
StringSum = StringSum + Mid(str, i, 1)
Next i
End Function

A1: 1234
B1: =StringSum(A1)
B1 should display 10
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top