Forum

Using Regex in a VB...
 
Notifications
Clear all

Using Regex in a VBA macro to add a 0 before digits in a title

2 Posts
2 Users
0 Reactions
73 Views
(@armando817)
Posts: 1
New Member
Topic starter
 

I am trying to add a 0 before two digits in a title across hundreds of rows. I am trying to use VBA regex. There is something I am not understanding about regex. This is what a title looks like:

The BigBook, Vol. 1, No. 1   <== The end result should look like ==> The BigBook, Vol. 01, No. 01 (there are 1 - 9 Vol. nums and No. nums)

Here is my test code so far.

Sub RegexReplace1()
Dim stringOne As String
Dim regexOne As Object

Set regexOne = New RegExp

regexOne.Pattern = "Vol. [0-9], No. [0-9]"
regexOne.Global = True
stringOne = "The BigBook, Vol. 1, No. 1"

Debug.Print regexOne.replace(stringOne, "Vol. 0[0-9], No. 0[0-9]")
End Sub

I think I'm close...but can't seem to figure this out. Any help would be appreciated. Thanks.

 
Posted : 16/11/2020 12:51 am
Philip Treacy
(@philipt)
Posts: 1630
Member Admin
 

Hi Armando,

I'd just use SUBSTITUTE

str = WorksheetFunction.Substitute(Range("A1"), ". ", ". 0")

where your title is in A1.

Regards

Phil

 
Posted : 16/11/2020 10:22 pm
Share: