Option Explicit
Function ExtractNumber(str As String) As String
Dim p As Long, c As String, out As String
ExtractNumber = ""
If Len(str) = 0 Then Exit Function
p = 1
Do
c = Mid(str, p, 1)
If InStr(":=(),;+-/*&%", c) > 0 Then
p = p + 1
ElseIf c = """" Then
Call SkipQuoteString(str, p)
ElseIf UCase(c) >= "A" And UCase(c) <= "Z" Then
Call SkipAlphanumeric(str, p)
ElseIf IsNumeric(c) Then
out = out & GetNumeric(str, p) & ","
Else
MsgBox ("Bad characters '" & c & "' at position " & p & " of string " & str)
Exit Function
End If
Loop Until p > Len(str)
ExtractNumber = out
End Function
Sub SkipQuoteString(ByRef str As String, ByRef p As Long)
Dim c As String
Do
p = p + 1
Loop Until p > Len(str) Or Mid(str, p, 1) = """"
If Mid(str, p, 1) = """" Then p = p + 1
End Sub
Sub SkipAlphanumeric(ByRef str As String, ByRef p As Long)
Do
p = p + 1
Loop Until (p > Len(str)) Or Not AlphaNumeric(Mid(str, p, 1))
End Sub
Function GetNumeric(ByRef str As String, ByRef p As Long) As String
Dim b As Long
b = p
Do
p = p + 1
Loop Until p > Len(str) Or Not IsNumeric(Mid(str, p, 1))
GetNumeric = Mid(str, b, p - b)
End Function
Function AlphaNumeric(ByVal x As String) As Boolean
AlphaNumeric = False
If x >= "A" And x <= "Z" Then AlphaNumeric = True
If x >= "a" And x <= "z" Then AlphaNumeric = True
If x >= "0" And x <= "9" Then AlphaNumeric = True
End Function
Sub Test_ExtractNumber()
Dim t As String
t = ExtractNumber("=A1-5")
t = ExtractNumber("=A1+6-B2-7&"" Cash""")
t = ExtractNumber("=(SUM(D15:D17)-F218-75)/(D19*12)*DATEDIF(C14,E20,""m"")+108")
End Sub