Extract Dollar value from a text String

Kyosti

Board Regular
Joined
Jun 2, 2008
Messages
90
Hello all,
It has been awhile since I have asked for help, but I need a quick hand. I need to extract a dollar value from the text below.


“I spoke with F on phone number f regarding the status of policy number f. I advised that current amount due of $10. includes a past due amount of $10. that is due before 8/14 to avoid cancellation of the policy. To prevent a loss of coverage, F made a payment of $20.0 with (CPay). The reference number for the payment made is (reference).”


I need to be able to pull the different dollar values based on the text that precedes them. For example I need one for the “amount due”, another for the “Past due” and the last for the “Payment of”. This way I can extract the three different values into 3 different cells. The verbiage is the criteria because it will always be the same.


I tried searching for something that would help and I found this but it will only return the first value found, and I cannot figure out how to adjust it.


=MID(B4,FIND("$",B4),FIND(" ",B4&" ",FIND("$",B4))-FIND("$",B4))+0


Thanks again in advance.
 
Actual there was a change in your text. In your first example you had the text "with (" after the last amount. In your second example it's just "with".
The formula from jtakw is probably the better way to go, but my formula for the payment could be changed to:
Excel Workbook
BCD
4I spoke with Customer Name on phone number 555-555-5555 regarding the status of policy number 123456789. I advised that current amount due of $28.03 includes a past due amount of $14.02 that is due before 8/16/2018 to avoid cancellation of the policy. To prevent a loss of coverage, Customer Name made a payment of $104.11 with a Visa. The reference number for the payment made is 123456789.
5
6
7Amount Due28.03
8Past Due14.02
9Payment104.11
Sheet
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This is one more approach using UDF (User Defind Function) if interested ...

Code:
Function Extract_Amounts(Txt As String, Seq As Integer) As Double
Dim Arr() As Variant, Ref As String, Cnt As Integer, ArCnt As Integer
Cnt = Len(Txt) - Len(Replace(Txt, "$", ""))
ReDim Arr(1 To Cnt)
For x = 1 To Len(Txt)
    If Mid(Txt, x, 1) = "$" Then
        Cnt = x + 1
        Do While Mid(Txt, Cnt, 1) <> " "
            Ref = Ref & Mid(Txt, Cnt, 1)
            Cnt = Cnt + 1
        Loop
            ArCnt = ArCnt + 1
            Arr(ArCnt) = Ref
            Ref = ""
    End If
Next x
Extract_Amounts = Arr(Seq)
End Function

The UDF could be used as follows


Book1
ABCD
1Text PhraseAmount 1Amount 2Amount 3
2I spoke with F on phone number f regarding the status of policy number f. I advised that current amount due of $10. includes a past due amount of $10. that is due before 8/14 to avoid cancellation of the policy. To prevent a loss of coverage, F made a payment of $20.0 with (CPay). The reference number for the payment made is (reference).101020
3I spoke with Customer Name on phone number 555-555-5555 regarding the status of policy number 123456789. I advised that current amount due of $28.03 includes a past due amount of $14.02 that is due before 8/16/2018 to avoid cancellation of the policy. To prevent a loss of coverage, Customer Name made a payment of $104.11 with a Visa. The reference number for the payment made is 123456789.28.0314.02104.11
Sheet1
Cell Formulas
RangeFormula
B2=Extract_Amounts(A2,1)
B3=Extract_Amounts(A3,1)
C2=Extract_Amounts(A2,2)
C3=Extract_Amounts(A3,2)
D2=Extract_Amounts(A2,3)
D3=Extract_Amounts(A3,3)
 
Upvote 0
So after toiling with the formulas for a few hours, I have discovered that both work well. The cause of the issues were with the verbiage that changed slightly if there was no past due amount input.

Thank you both for the help
 
Upvote 0
If you show a couple of samples where there is no past due amount ( or payment amount, or even amount due ), I can probably tweak my formula to work.
 
Upvote 0
This is one more approach using UDF (User Defind Function) if interested ...

Code:
Function Extract_Amounts(Txt As String, Seq As Integer) As Double
Dim Arr() As Variant, Ref As String, Cnt As Integer, ArCnt As Integer
Cnt = Len(Txt) - Len(Replace(Txt, "$", ""))
ReDim Arr(1 To Cnt)
For x = 1 To Len(Txt)
    If Mid(Txt, x, 1) = "$" Then
        Cnt = x + 1
        Do While Mid(Txt, Cnt, 1) <> " "
            Ref = Ref & Mid(Txt, Cnt, 1)
            Cnt = Cnt + 1
        Loop
            ArCnt = ArCnt + 1
            Arr(ArCnt) = Ref
            Ref = ""
    End If
Next x
Extract_Amounts = Arr(Seq)
End Function
Here is another UDF that could be used. It takes two arguments... the first is the text to be searched and the second is one of these three prhases... "amount due", "past due" or "payment of" (I envision those three phrases to be headers in cells that the UDF formula references).
Code:
[table="width: 500"]
[tr]
	[td]Function ExtractAmount(Txt As String, Phrase As String) As Double  ExtractAmount = Val(Split(Split(Txt, Phrase, , vbTextCompare)(1), "$")(1))
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
That's very clever (and short :)) as always Rick ! Just one question though, what does the (1) after the spilt function do ?

Here is another UDF that could be used. It takes two arguments... the first is the text to be searched and the second is one of these three prhases... "amount due", "past due" or "payment of" (I envision those three phrases to be headers in cells that the UDF formula references).
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function ExtractAmount(Txt As String, Phrase As String) As Double  ExtractAmount = Val(Split(Split(Txt, Phrase, , vbTextCompare)(1), "$")(1))
End Function[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
That's very clever (and short :)) as always Rick ! Just one question though, what does the (1) after the spilt function do ?
Split returns a zero-based array, so the (1) retrieves the second element from that array it creates (which is the text after the text used as the delimiter). You may be thinking that you need to assign the output from the Split function to a String array variable or, alternately, a Variant variable so that you can then retrieve elements from the array that gets assigned to that variable. Of course, that would work, and if you needed to retrieve multiple array elements, it would be the way to code it; however, since we only need to retrieve one element from the array Split returns, you can pull that element out directly from the Split function as the function is returning a real one-dimension, zero-based array.
 
Upvote 0
Thanks Rick for the clarification. I just tried (0) instead of (1) & it retuned the first element of the array 8-) … That's insightful
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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