VBA Regular Expressions Find and Replace

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to mimic a text editor that I am using for regular expressions in VBA. The text editor I am using is Sublime. I have a bunch of phrases and the numeric values I would like to put a dollar sign in front if I do not already have one. for example:

I have this:

The $40.00 dollar amount is ok with 10.00 down
The 87.00 is what we asked for.
Is Jon getting 3500.00 for his car.
Can I borrow 10.00 until Monday. I will pay you back 15.00.
Is the money for the hat 20.00?
The 11.00 dollar amount is 10.00 ok

using the regex pattern

(?<![$\d])(?=\d+\.\d\d)


and replacing with a $

I get this:

The $40.00 dollar amount is ok with $10.00 down
The $87.00 is what we asked for.
Is Jon getting $3500.00 for his car.
Can I borrow $10.00 until Monday. I will pay you back $15.00.
Is the money for the hat $20.00?
The $11.00 dollar amount is $10.00 ok

Is there a way to do this in VBA??

Thanks in advance!
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Lightly tested, but returns the results you provided for the examples you provided.
Code:
Sub AddDollarSignToNumbers()
'assume strings are in col A starting in A1
Dim c As Range, V As Variant, i As Long
Application.ScreenUpdating = False
For Each c In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    V = Split(c.Value, " ")
    For i = 0 To UBound(V)
        If V(i) Like "[0-9]*" Then V(i) = "$" & V(i)
    Next i
    c.Value = Join(V, " ")
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you JoeMo that definitely did the job. Appreciate it! Thank you also JLGWhiz for the link. Thank you both again!
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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