Extract Numbers From Text String And Sum Them

simona123

New Member
Joined
Aug 26, 2017
Messages
10
[TABLE="width: 134"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Dear friends, I need help.I have this:
A1: 6-2, 6-2[/TD]
[/TR]
[TR]
[TD]A2: 8-1, 4-3[/TD]
[/TR]
[TR]
[TD]A3: 6-2, 6-3,5-3
[/TD]
[/TR]
[TR]
[TD]And I want to sum all the numbers before the "-"
Example: In B1 i want to have 12 (6+6) from A1.[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]



[TR]
[TD][TABLE="width: 134"]
<tbody>[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]

</tbody>[/TABLE]
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Before someone creates a shorter formula, try the following;

In cell B1 enter the formula:

Code:
=SUMPRODUCT(1*MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1;"-";"");",";"");" ";"");ROW(INDIRECT("1:"&LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1;"-";"");",";"");" ";""))));1))


You may need to change semicolons(";") to commas (",") depending on your Excel settings.
 
Last edited:
Upvote 0
Code:
=SUMPRODUCT(1*MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1;"-";"");",";"");" ";"");ROW(INDIRECT("1:"&LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1;"-";"");",";"");" ";""))));1))

You may need to change semicolons(";") to commas (",") depending on your Excel settings.
Your formula appears to be adding up all of the digits... the OP asked to add up the number in front of each dash.
 
Upvote 0
I am pretty sure there is a more compact formula to be had, but until someone posts it, give this one a try...

=IFERROR(SUMPRODUCT(0+LEFT(TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",100)),ROW(INDIRECT("1:"&(LEN(A1)-LEN(SUBSTITUTE(A1,"-","")))))*100,100)),FIND("-",TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",100)),ROW(INDIRECT("1:"&(LEN(A1)-LEN(SUBSTITUTE(A1,"-","")))))*100,100)))-1)),"")


If you are looking for a VBA solution instead, then here is a UDF (user defined function) that you can try...
Code:
Function SumBeforeMinus(S As String) As Variant
  Dim V As Variant
  For Each V In Split(S, ",")
    SumBeforeMinus = SumBeforeMinus + Val(V)
  Next
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use SumBeforeMinus just like it was a built-in Excel function. For example,

=SumBeforeMinus(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
If there is some smallish limit on how many sets of scores in a cell (I've used a limit of 6), you could also try this.


Book1
AB
16-2, 6-212
28-1, 4-312
36-2, 6-3,5-317
42-4, 15-1,12-13, 1-030
Sum Before Dash
Cell Formulas
RangeFormula
B1=SUMPRODUCT(IFERROR(0+MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),",","-"),"-",REPT(" ",100)),{1,3,5,7,9,11}*100-99,99),0))
 
Upvote 0
Thank you for helping me! It works.
Glad you have something suitable. Not knowing which method suits you best though, if you are pursuing the UDF path, here is a one-liner for you to consider.

Code:
Function SumPreDash(s As String) As Double
  SumPreDash = Evaluate(Replace(Replace(s, ",", ")+"), "-", "-N(N1") & ")")
End Function


Book1
AB
16-2, 6-212
28-1, 4-312
36-2, 6-3,5-317
42-4, 15-1,12-13, 1-830
518-1918
Sum Before Dash
Cell Formulas
RangeFormula
B1=SumPreDash(A1)
 
Upvote 0
Hi

Another option for a udf:

Code:
Function AddLeft(s As String) As Double
    AddLeft = Evaluate(Replace(Replace(s, ",", "+"), "-", "-0*"))
End Function
 
Upvote 0
Another option for a udf:

Code:
Function AddLeft(s As String) As Double
    AddLeft = Evaluate(Replace(Replace(s, ",", "+"), "-", "[COLOR="#FF0000"][B]-0*[/B][/COLOR]"))
End Function
Doh, why couldn't I think of that simple solution instead of the clumsy thing I came up with? :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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