Adding numbers in String

GregDaw

New Member
Joined
Jun 10, 2003
Messages
2
I have the need to add the numbers in a string (i.e. 76432 = 22, 8121=12). This is used as a confiramtion code. The number resides in one cell and I would like a new column with the results. Any Ideas?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can write a user defined function in VBA to do that quite easily. Here is the code:

Code:
Function SumNum(MyCell As Range) As Integer

    Dim MyLen, i As Integer
    
    MyLen = Len(MyCell)
    For i = 1 To MyLen
        SumNum = SumNum + Mid(MyCell, i, 1)
    Next i
    
End Function

Then, if the number is in cell A1, then in cell B1, enter the following:
=SUMNUM(A1)
 
Upvote 0
Hello, if you want to do this in a worksheet, I think I would use Aladin's worksheet function over a vba udf.

But if you wanted to do this in VB for whatever reason, I might be tempted to stack a byte array and use it like:<font face=Courier New><SPAN style="color:darkblue">Function</SPAN> AddInp(myStr<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>)<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Integer</SPAN><SPAN style="color:darkblue">Dim</SPAN> b()<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Byte</SPAN>, z<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Integer</SPAN>, i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Integer</SPAN>
b = myStr<SPAN style="color:darkblue">For</SPAN> i =<SPAN style="color:darkblue">LBound</SPAN>(b)<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(b)<SPAN style="color:darkblue">Step</SPAN> 2
     z = z + Val(ChrW$(b(i)))<SPAN style="color:darkblue">Next</SPAN>
Erase b
AddInp = z
z =<SPAN style="color:darkblue">Empty</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Function</SPAN><SPAN style="color:darkblue">Sub</SPAN> test()
MsgBox AddInp("76432")
MsgBox AddInp("764 32")
MsgBox AddInp("764 test 32")<SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Sub</SPAN></FONT>

I use the val statement to compensate for non numerical characters in your string. You could use it in a worksheet like:
Book3
ABCD
11236
21test23
Sheet1


But again, I might stick with the native functionality presented earlier unless you have text characters in your string.
 
Upvote 0
Thank you all for the good imput. I had spent a good two hours on it before you all saved my sanity.

Greg
 
Upvote 0
Meant to post this last week, and wouldn't bother except that the Excel formula below handles just about everything. I haven't seen this formula used elsewhere, and this topic comes up from time to time (for example, see thread: http://www.mrexcel.com/board2/viewtopic.php?t=52283). I struggled with it myself, then came upon this formulation while working on another post (http://www.mrexcel.com/board2/viewtopic.php?p=239317#239317).

The non-array formula in cell B2 is:

=SUM((LEN(A2)-LEN(SUBSTITUTE(A2,{1,2,3,4,5,6,7,8,9},"")))*{1,2,3,4,5,6,7,8,9})

I also included a column for NateO's excellent UDF, which provides the same answers.

--Tom
MrExcel61703.xls
ABCD
1ItemSumDigitsNotesAddInp()
2764322222
3(8,121)1212
4764322222
5764test322222
61test233
7156.5462727
83,127,543.79124444
9t=18317;p=2393174545
1024/1299
113054'43"1919
12SSN421-89-73223838
13800/555-12122929
143.1415926535897977PI()displayedwith14decimals77
1533/3227underlyingvalueisof33/32is3.0937527
166:00PM12underlyingvalueof6:00PMis0.7512
SumDigits
 
Upvote 0
Looks like a keeper, robust, nice work Tom. They both are functional against (glad mine worked as advertised :LOL: ):

'$22230 as well.

Looks like the way to go in the thread you linked to as well. Kudos & have a nice weekend.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top