VBA extract numbers from string

opislak

Board Regular
Joined
Feb 28, 2017
Messages
68
Hello all,

I get e-mails from people who have to hand me numbers of man-days or man-hours. Is there a way using VBA to:
- put the string in a cell
- extract only the numbers from the strings I get
- yet include the plus-sign "+"
- replace the American-style dot with a comma
- keep the different numbers a part (not added)
- put the result back in the same cell

Example: if I get this string and put in in cell B1:
3MD+1.5md + 4.2 days
I would like to get this result, regardless of use of capitals or other ("MD" or "days" or "d" or whatever) and regardless of the use of a dot or a comma, again in cell B1:
= 3 + 1,5 + 4,2
of course, the result shown in the cell would be the addition of the numbers (8,7), which is fine, only in the formula bar, one would be able to see where this result comes from.
The use of TEXTJOIN doesn't give me what I want (it would show 31542)

Many thanks for your input.
Pat.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Check if the following helps you.
One function is to obtain the equation. Another function to obtain the result of the sum.

VBA Code:
Function extractnumbers(s As String)
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^1-9.+]"
    .Global = True
    extractnumbers = "=" & Replace(.Replace(s, ""), ".", ",")
  End With
End Function

Function sumnumbers(s As String)
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^1-9.+]"
    .Global = True
    sumnumbers = Evaluate("=" & .Replace(s, ""))
  End With
End Function

Dante Amor
ABCD
13MD+1.5md + 4.2 days=3+1,5+4,28.7
2
Hoja2
Cell Formulas
RangeFormula
C1C1=extractnumbers(B1)
D1D1=sumnumbers(B1)
 
Upvote 0
One detail, I added the zero in the extraction:

VBA Code:
Function extractnumbers(s As String)
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9.+]"
    .Global = True
    extractnumbers = "=" & Replace(.Replace(s, ""), ".", ",")
  End With
End Function

Function sumnumbers(s As String)
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9.+]"
    .Global = True
    sumnumbers = Evaluate("=" & .Replace(s, ""))
  End With
End Function
 
Upvote 0
Solution
One detail, I added the zero in the extraction:

VBA Code:
Function extractnumbers(s As String)
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9.+]"
    .Global = True
    extractnumbers = "=" & Replace(.Replace(s, ""), ".", ",")
  End With
End Function

Function sumnumbers(s As String)
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9.+]"
    .Global = True
    sumnumbers = Evaluate("=" & .Replace(s, ""))
  End With
End Function
This works fine, thank you very much. Have a nice day!
 
Upvote 0
For those who might be interested, here is an alternate way to sum the numbers in OP's text which does not call out to a Regular Expression engine...
VBA Code:
Function SumNumbers(ByVal S As String)
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(s, X, 1) Like "[!0-9.+]" Then Mid(S, X) = " "
  Next
  SumNumbers = Evaluate(S)
End Function
 
Upvote 0
For those who might be interested, here is an alternate way to sum the numbers in OP's text which does not call out to a Regular Expression engine...
VBA Code:
Function SumNumbers(ByVal S As String)
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(s, X, 1) Like "[!0-9.+]" Then Mid(S, X) = " "
  Next
  SumNumbers = Evaluate(S)
End Function
Works fine too.
 
Upvote 0

Forum statistics

Threads
1,215,476
Messages
6,125,029
Members
449,205
Latest member
Eggy66

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