GetNumeric Function issue

Trying2learnVBA

Board Regular
Joined
Aug 21, 2019
Messages
67
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,

I would like to extract the number after USD in this string:
03-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00 DB

I've tried different methods, formulas, some vba methods.

this is my current code and I can't get it to work. I get an error. It says variable not defined and the 'i' gets highlighted. Any ideas as to what i am doing wrong?

VBA Code:
Function GetNumeric(CellRef As String)
Dim StringLength As Integer
StringLength = Len(CellRef)
For i = 1 To StringLength
If IsNumeric(Mid(CellRef, i, 1)) Then Result = Result & Mid(CellRef, i, 1)
Next i
GetNumeric = Result
End Function
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about:
Book1
AB
103-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00 DB127575
2 
303-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00127575
Sheet3
Cell Formulas
RangeFormula
B1:B3B1=ExtractUSDAmount(A1)

VBA Code:
Function ExtractUSDAmount(S As String) As Variant
Dim V As Variant
If S = "" Or InStr(S, "USD") = 0 Then
    ExtractUSDAmount = ""
    Exit Function
End If
V = Split(S, " ")
For i = LBound(V) To UBound(V) - 1
    If V(i) = "USD" Then
        If IsNumeric(V(i + 1)) Then
            ExtractUSDAmount = Val(Replace(V(i + 1), ",", ""))
            Exit For
        End If
    End If
Next i

End Function
Format cells in col B to suit (for example as currency).
 
Upvote 0
I've tried different methods, formulas
We might need more than one example to make the formula(s) robust, but what about these for a start?
Format the result cell with whatever format you want.

Trying2learnVBA.xlsm
ABC
103-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00 DB127575127575
Sheet1
Cell Formulas
RangeFormula
B1B1=TEXTBEFORE(TEXTAFTER(A1," USD ")," ")+0
C1C1=LET(f,FIND(" USD ",A1),REPLACE(LEFT(A1,FIND(" ",A1,f+6)),1,f+3,"")+0)
 
Upvote 0
We might need more than one example to make the formula(s) robust, but what about these for a start?
Format the result cell with whatever format you want.

Trying2learnVBA.xlsm
ABC
103-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00 DB127575127575
Sheet1
Cell Formulas
RangeFormula
B1B1=TEXTBEFORE(TEXTAFTER(A1," USD ")," ")+0
C1C1=LET(f,FIND(" USD ",A1),REPLACE(LEFT(A1,FIND(" ",A1,f+6)),1,f+3,"")+0)

Hey thank you for this - C1 formula works great. B1 formula does not work for me. I get #NAME something with my syntax. But C1 is a fine solution. Thank you again
 
Upvote 0
How about:
Book1
AB
103-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00 DB127575
2 
303-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00127575
Sheet3
Cell Formulas
RangeFormula
B1:B3B1=ExtractUSDAmount(A1)

VBA Code:
Function ExtractUSDAmount(S As String) As Variant
Dim V As Variant
If S = "" Or InStr(S, "USD") = 0 Then
    ExtractUSDAmount = ""
    Exit Function
End If
V = Split(S, " ")
For i = LBound(V) To UBound(V) - 1
    If V(i) = "USD" Then
        If IsNumeric(V(i + 1)) Then
            ExtractUSDAmount = Val(Replace(V(i + 1), ",", ""))
            Exit For
        End If
    End If
Next i

End Function
Format cells in col B to suit (for example as currency).

Hey this seems to work perfectly for you. However, i am getting the same error I get with the code I posted originally.
"Variable not defined" and the "i" is highlighted. I do not see a dim for "i" do you have this variable defined in another workbook? how should I define "i"?
 
Upvote 0
Hey this seems to work perfectly for you. However, i am getting the same error I get with the code I posted originally.
"Variable not defined" and the "i" is highlighted. I do not see a dim for "i" do you have this variable defined in another workbook? how should I define "i"?
Try adding: Dim i as Long after the Dim V as Variant line.
 
Upvote 0
B1 formula does not work for me. I get #NAME
Sounds like you just don't have the TEXTBEFORE and TEXTAFTER functions yet. You should get them in due course.

For another user-defined function, based on your one data sample, you could also try this one.
It includes an option argument if you wanted to find currencies other than USD - eg Australian dollars in row 4 below

VBA Code:
Function CurrencyAmount(s As String, Optional CurrencyCode As String = "USD")
  CurrencyAmount = Val(Split(Replace(s, ",", "") & " " & CurrencyCode & " 0", " " & CurrencyCode & " ")(1))
End Function

Trying2learnVBA.xlsm
AB
103-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 USD 127,575.00 DB127575
203-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 GBP 127,575.00 DB0
3
403-Oct-2022 Swap Net Coupon CME IRS Trade: 15360193 03-Oct-2022 AUD 68,234.99 DB68234.99
Sheet3
Cell Formulas
RangeFormula
B1:B2B1=CurrencyAmount(A1)
B4B4=CurrencyAmount(A4,"AUD")
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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