Add From String

mgirvin

Well-known Member
Joined
Dec 15, 2005
Messages
1,236
Office Version
  1. 365
Platform
  1. Windows
Dear Smartest Excelers In The World,

I have 111.12 in cell A1, and there could be ending or leading spaces.

I want to add the digits like this1+1+1+1+2 to get 6

I have used this formula (entering it with the key strokes Ctrl + Shift + Enter):

=SUM(MID(TRIM(SUBSTITUTE(A1,".","")),ROW(INDIRECT("1:"&LEN(TRIM(SUBSTITUTE(A1,".",""))))),1)+0)

Is there a shorter formula or am I on the right rack?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
There are array and volatile formulas that do this, but here is a UserDefinedFunction that seems to make simple work in realtime, if you already have macros enabled, this may work for you.

First add this function:
Code:
Function AddDigits(NumberString As String) As Integer
  
  AddDigits = 0
  For i = 1 To Len(NumberString)
    CurDigit = Mid(NumberString, i, 1)
    If IsNumeric(CurDigit) Then AddDigits = AddDigits + CurDigit
  Next i

End Function
(source)

Press Alt-F11 to open editor
Insert > Module
Paste in code above
Alt-F11 to close editor
Save your sheet.

Now use that function as =AddDigits(A1)
 
Upvote 0
With
A1: (a number with leading and/or trailing spaces)

If the embedded numbers may contain a decimal point
AND
- will always be postive
- will always have 15 significant digits or less

Try this regular formula sums the digits:
Code:
B1: =SUMPRODUCT(--MID(TEXT(SUBSTITUTE(A1,".",""),REPT("0",15)),
ROW(INDIRECT("1:15")),1))

If A1: ' 123456.789
the formula returns: 45
...the sum of {1,2,3,4,5,6,7,8,9}

Is that something you can work with?
 
Upvote 0
Dear jbeaucaire,

Totally Cool. That works like a charm! And logically so simple.



Dear Ron Coderre,

Yes, I can work with that! I have not seen your TEXT solution to get rid of spaces before. I love learning new tricks! Also, your formula reminds me to use SUMPRODUCT. I still have natural reflexes to use SUM and CTRL + Shift + Enter.

Totally EXCELlent!!!!
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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