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.

# Summing Digits in a Value

Written by Allen Wyatt (last updated January 4, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003

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.

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
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
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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

##### Author Bio

Allen Wyatt

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. ...

##### MORE FROM ALLEN

Using the XIRR Function

One of the financial worksheet functions provided in Excel is the XIRR function. This is used to figure out an internal ...

Discover More

Splitting Table Cells

When formatting tables, you can both merge and split cells. Here's a couple of ways you can easily perform the latter ...

Discover More

Symbols Convert to Numbers in Excel

Insert a symbol into a cell, and it should stay there, right? What if the symbol changes to another character, such as a ...

Discover More

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!

How Operators are Evaluated

Operators are used in formulas to instruct Excel what to do to arrive at a result. Not all operators are evaluated in the ...

Discover More

Counting Cells According to Case

Text placed in cells can either be lowercase, uppercase, or a mixture of the two. If you want to count the cells based ...

Discover More

Counting Employees in Classes

Excel is very good at counting things, even when those things need to meet specific criteria. This tip shows how you can ...

Discover More
##### Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. Youâ€™ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 7?

2021-04-11 05:30:20

Willy Vanhaelen

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)
Next
End Function

2021-04-10 09:40:38

Roy

You can also use that approach for stripping out non-numeric characters leaving you with a numeric-only string, or value:

=VALUE(TEXTJOIN("",TRUE,IFERROR(VALUE(MID(A1,SEQUENCE(LEN(A1)),1)),"")))

So "D-55a4" becomes just "554" or just the string "554" if you don't apply the VALUE() fucntion at the end. "-8" becomes "8": you won't retain the idea something was a negative number, but since the digits were your focus, not their values, that isn't an issue.

Side note: if you ever have problems with ABS(), or more likely INT() and related functions, not returning a value axatly as you'd like, it could help you control that better (or not, depending on what your desire is).

Works nicely for stripping non-printing characters from what should be numerical information. Lots of imported data suffers from those and CLEAN() is usually useless for that problem. (Very much a "it was useful in simpler times" kind of function.)

It works with anything Excel builds VALUE() to recognize (just the usual for us in the US, but some other country builds might have VALUE() fucntions that recognize more characters as numerals, and so...).

2021-04-10 09:24:41

Roy

The following formula looks at the presented value character by character, applies the VALUE() as a simple test of each being a numerica character, uses IFERROR() to return a 0 to SUM() if any character is not a numeral, and taking the result if a character is, letting SUM() total them all up:

=SUM(IFERROR(VALUE(MID(A1,SEQUENCE(LEN(A1)),1)),0)) (for an imput presented in cell A1)

So only numerals are added, no matter what, and no complications. No macros needed. Something either succeeds inside VALUE() and is added, or does NOT succeed and is effectively ignored by the value used for it being 0.

##### This Site

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.