Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Summing Digits in a Value

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), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Summing Digits in a Value.

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:

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Jaan    06 Aug 2016, 23:08
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
Haris    26 Jan 2016, 00:02
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
Willy Vanhaelen    12 Apr 2015, 12:45
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
JOAN SQUIRES    11 Apr 2015, 06:18
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.
awyatt    14 May 2013, 09:25
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
Tzvetin Kmeski    13 May 2013, 11:20
Hello,

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

How this tip will looks for excel 2010?

Thanks,

Tzvetin.
Rajesh    10 Jan 2013, 06:46
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
Keith Griffiths    26 Dec 2012, 06:31
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.

Rick Rothstein    22 Aug 2012, 13:23
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.
Armando Montes    11 Aug 2012, 08:17
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)
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.