Loading

Got a version of Excel that uses the
menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)?
**This site is for you!** If you
use a later version of Excel, visit
our *ExcelTips* site focusing on the ribbon interface.

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.

Learn more about Allen...

ExcelTips FAQ

ExcelTips Resources

Ask an Excel Question

Make a Comment

Free Business Forms

Free Calendars

** Please Note:** This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later),

If you have a cell that contains a value, you may want to devise a way to add together all the digits in the value. For instance, if a cell contains the value 554, you might want to determine the sum of 5+5+4, which is 14.

There are several ways you can approach this task. (Doesn't that always seem the way in Excel?) The first is to use a formula that relies on several functions:

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

This regular formula will sum the digits in any integer value (in cell A1) in a simple, elegant manner. This is not the only possible formula, however. The following is an array formula (terminated by pressing **Ctrl+Shift+Enter**) version of the same formula:

=SUM(1*MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))

Either of these formulas work fine if the value in A1 is a positive whole number. If there are any non-digit characters in the number (such as a negative sign or a decimal point), then the formulas return a #VALUE! error.

These are not the only formulas possible for this type of calculation. You can find some other examples of formulas in the Microsoft Knowledge Base:

http://support.microsoft.com/kb/214053

You can also use a user-defined function to return the desired sum. The following macro steps through each digit in the referenced cell and calculates a total. This value is then returned to the user:

Function AddDigits(Number As Long) As Integer Dim i As Integer Dim Sum As Integer Dim sNumber As String sNumber = CStr(Number) For i = 1 To Len(sNumber) Sum = Sum + Mid(sNumber, i, 1) Next AddDigits = Sum End Function

To use this function, just use a formula such as =AddDigits(A1) in a cell. An even more compact user-defined function (invoked in the same manner) is the following:

Function AddDigits(ByVal N As Long) As Integer Do While N >= 1 AddDigits = AddDigits + N Mod 10 N = Int(N / 10) Loop End Function

Unlike the earlier macro, this version doesn't convert the cell contents to a string in order to process it. Instead, it steps through each digit of the value, stripping off the last digit and adding it to the total.

*ExcelTips* is your source for cost-effective Microsoft Excel training. This tip (2424) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Summing Digits in a Value.

*Related Tips:*

**Professional Development Guidance!** Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out *Professional Excel Development* today!

If possible you can help me in writing a formula for the following:

SUM OF TWO CELLS A1 AND C1 FOR EXAMPLE IS 1234 IN CELL B2. NOW I WANT IN THE SAME CELL B2 FIRST THREE DIGITS SHOULD REMOVE LEAVING THE DIGIT 4 ONLY IN CELL B2.

YOUR HELP WILL HIGHLY APPRECIATED.

THANK YOU SO MUCH.

JAAN

SUM OF TWO CELLS A1 AND C1 FOR EXAMPLE IS 1234 IN CELL B2. NOW I WANT IN THE SAME CELL B2 FIRST THREE DIGITS SHOULD REMOVE LEAVING THE DIGIT 4 ONLY IN CELL B2.

YOUR HELP WILL HIGHLY APPRECIATED.

THANK YOU SO MUCH.

JAAN

Hi there,

I'm experiencing trouble with having too large of numbers for Excel to handle. To give you an example, I need Excel to handle a number such as the following without rounding it off:

Number I need - > 0.00000067055225372314453125 - > Sum of all digits = 72

Excel Outputs - > 0.00000067055225372314500000 - > Sum of all digits = 56

Excel rounds off digits which skews my data by a lot because I need to have the sum of all digits of the entire number.

I need 2 things:

1. 0.00000067055225372314453125 / 2 - > simply to be able to divide a large format number such as this one by 2 and get the actual result by somehow manipulating it to be in text format. It needs to be a text format to begin with, treat it as a number and divide it by 2 then convert back to text format so that all the digits actually exist.

2. Then I need to get the sum of all the digits of the result in one particular cell that unfortunately has to be formatted as text in order to have all the digits.

This would be easier if the cell was formatted as a number, but the number ends up too large for excel to handle so my result ends up wrong.

I hope this makes sense :)

Thanks so much in advanced,

Haris

I'm experiencing trouble with having too large of numbers for Excel to handle. To give you an example, I need Excel to handle a number such as the following without rounding it off:

Number I need - > 0.00000067055225372314453125 - > Sum of all digits = 72

Excel Outputs - > 0.00000067055225372314500000 - > Sum of all digits = 56

Excel rounds off digits which skews my data by a lot because I need to have the sum of all digits of the entire number.

I need 2 things:

1. 0.00000067055225372314453125 / 2 - > simply to be able to divide a large format number such as this one by 2 and get the actual result by somehow manipulating it to be in text format. It needs to be a text format to begin with, treat it as a number and divide it by 2 then convert back to text format so that all the digits actually exist.

2. Then I need to get the sum of all the digits of the result in one particular cell that unfortunately has to be formatted as text in order to have all the digits.

This would be easier if the cell was formatted as a number, but the number ends up too large for excel to handle so my result ends up wrong.

I hope this makes sense :)

Thanks so much in advanced,

Haris

Here is a user defined function that works with negative numbers and numbers with decimals as well:

Function AddDigits(Number As String) As Integer

Dim i As Integer, Char As String

For i = 1 To Len(Number)

Char = Mid(Number, i, 1)

If IsNumeric(Char) Then AddDigits = AddDigits + Char

Next

End Function

Function AddDigits(Number As String) As Integer

Dim i As Integer, Char As String

For i = 1 To Len(Number)

Char = Mid(Number, i, 1)

If IsNumeric(Char) Then AddDigits = AddDigits + Char

Next

End Function

Many thanks for this. I absolutely love playing with Excel and it is nice to know that when I get stuck there is an amazingly clever person like you out there to come to my rescue. Formula worked perfectly.

Tzvetin,

As this tip says, it is for Excel 97 through Excel 2003. If you want to see how it works in Excel 2010, click the link at the end of the tip which says "You can find a version of this tip for the ribbon interface..."

-Allen

As this tip says, it is for Excel 97 through Excel 2003. If you want to see how it works in Excel 2010, click the link at the end of the tip which says "You can find a version of this tip for the ribbon interface..."

-Allen

Hello,

My Excel version is 2010 and unfortunately this tip doesn't work.

How this tip will looks for excel 2010?

Thanks,

Tzvetin.

My Excel version is 2010 and unfortunately this tip doesn't work.

How this tip will looks for excel 2010?

Thanks,

Tzvetin.

Hey

Thanks for this, Excellent one.

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

I would like to explore withing the same formula, how can we get the result to one single digit or reduce to one single digit.

Rajesh

Thanks for this, Excellent one.

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

I would like to explore withing the same formula, how can we get the result to one single digit or reduce to one single digit.

Rajesh

From website:

http://excel.tips.net/T002424_Summing_Digits_in_a_Value.html

there is this formula for producing sums of digits in a value:

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

However I would like to multiply the digits of the value within a cell. i.e. if A1 is 456 a formula would produce 120. I recognize that a zero digit would make the result zero so the formula would have to ignore zeros. to get the result of the remaining non-zero digits.

Would appreciate any ideas.

http://excel.tips.net/T002424_Summing_Digits_in_a_Value.html

there is this formula for producing sums of digits in a value:

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

However I would like to multiply the digits of the value within a cell. i.e. if A1 is 456 a formula would produce 120. I recognize that a zero digit would make the result zero so the formula would have to ignore zeros. to get the result of the remaining non-zero digits.

Would appreciate any ideas.

There is a one-liner UDF (user defined function) that can do this...

Function DigitSum(S As String) As Long

DigitSum = Evaluate(Replace(StrConv(S, vbUnicode), Chr(0), "+") & "0")

End Function

Obviously, large "numbers" have to be formatted as text. The function will successfully handle a text string of up to 127 digits.

@Armando and Houdini,

That UDF and those formulas do not do the same thing as the UDF that Allen posted... Allen's UDF (and the one I posted above) sums the digits whereas as yours do a "casting out 9's" summation of digits... they are not the same thing.

Function DigitSum(S As String) As Long

DigitSum = Evaluate(Replace(StrConv(S, vbUnicode), Chr(0), "+") & "0")

End Function

Obviously, large "numbers" have to be formatted as text. The function will successfully handle a text string of up to 127 digits.

@Armando and Houdini,

That UDF and those formulas do not do the same thing as the UDF that Allen posted... Allen's UDF (and the one I posted above) sums the digits whereas as yours do a "casting out 9's" summation of digits... they are not the same thing.

Check this out:

http://www.mrexcel.com/forum/showthread.php?t=622330&page=2

Function Quick(ByVal number)

Quick = (number - 1) Mod 9 + 1

End Function

Same as formula

=MOD(A1-1,9)+1

HOUDINI:

That formula works for me but note that it will fail for values of A1 greater than 1207959552 (because MOD function returns an error in that case)

This version can cope with larger numbers

=A1-FLOOR(A1-1,9)

http://www.mrexcel.com/forum/showthread.php?t=622330&page=2

Function Quick(ByVal number)

Quick = (number - 1) Mod 9 + 1

End Function

Same as formula

=MOD(A1-1,9)+1

HOUDINI:

That formula works for me but note that it will fail for values of A1 greater than 1207959552 (because MOD function returns an error in that case)

This version can cope with larger numbers

=A1-FLOOR(A1-1,9)