Replacing number followed by word from string using Excel VBA

mathsbeauty

Board Regular
Joined
Apr 23, 2011
Messages
89
There are numbers (1 to 4 digit numbers < 1000) followed by the word "Page" in a string. I want to replace the numbers appearing immediately after the word "Page" by "xyz". So the resulting string should be as "abcd Page xyz dfdfsda Page xyz fsadfds Page xyz" when the original string is "abcd Page 32 dfdfsda Page 1 fsadfds Page 4567". It would be nice if we can construct a function in Excel VBA for the same. If replacement is not possible, then removing numbers would also be okay.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi
?
VBA Code:
Sub test()
Dim txt, x, t, i
   txt = "abcd Page 32 dfdfsda Page 1 fsadfds Page 4567"
    With CreateObject("VBScript.RegExp")
        .Pattern = "(\D+Page )"
        .Global = True
            Set x = .Execute(txt)
        For i = 0 To x.Count - 1
        t = t & x(i) & " XYZ "
        Next
    End With
    MsgBox t
End Sub
 
Upvote 0
Ahhh mohadin beat me to it. Ill just post my idea anyways since I spent some time on it and it might still be helpful
My idea:

For i = 0 to 9, replace "Page i" with "Page xyz"
Then, 3 times after this, do the following:
For i = 0 to 9, replace "Page xyzi" with "Page xyz" (note the purposeful spaces).

Sorry for not actually coding, I am quite new with VBA, but on paper the pseudocode should work.
 
Upvote 0
And
VBA Code:
Sub test2()
  
Sub test2()
    Dim txt, x, t, i
    txt = "abcd Page 32 dfdfsda Page 1 fsadfds Page 4567"
    With CreateObject("VBScript.RegExp")
        .Pattern = "\d{1,4}"
        .Global = True
      MsgBox .Replace(txt, " XYZ ")
    End With
End Sub
 
Upvote 0
Hi, here's another option you could also try.

VBA Code:
Function myReplace(s As String)
Dim v As Variant, i As Long
v = Split(s, " ")
For i = 1 To UBound(v)
    If v(i) = "Page" Then
        If 1 < UBound(v) Then
            If IsNumeric(v(i + 1)) Then v(i + 1) = "xyz"
        End If
    End If
Next i
myReplace = Join(v, " ")
End Function

Book1
AB
1abcd Page 32 dfdfsda Page 1 fsadfds Page 4567abcd Page xyz dfdfsda Page xyz fsadfds Page xyz
2abcd Page 32 dfdfsda Page 1 fsadfds Page 4567 pageabcd Page xyz dfdfsda Page xyz fsadfds Page xyz page
3abcd Page 32 dfdfsda Page abc fsadfds Page 4567 pageabcd Page xyz dfdfsda Page abc fsadfds Page xyz page
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=myReplace(A1)
 
Upvote 0
Solution
Hi, here's another option you could also try.

VBA Code:
Function myReplace(s As String)
Dim v As Variant, i As Long
v = Split(s, " ")
For i = 1 To UBound(v)
    If v(i) = "Page" Then
        If 1 < UBound(v) Then
            If IsNumeric(v(i + 1)) Then v(i + 1) = "xyz"
        End If
    End If
Next i
myReplace = Join(v, " ")
End Function

Book1
AB
1abcd Page 32 dfdfsda Page 1 fsadfds Page 4567abcd Page xyz dfdfsda Page xyz fsadfds Page xyz
2abcd Page 32 dfdfsda Page 1 fsadfds Page 4567 pageabcd Page xyz dfdfsda Page xyz fsadfds Page xyz page
3abcd Page 32 dfdfsda Page abc fsadfds Page 4567 pageabcd Page xyz dfdfsda Page abc fsadfds Page xyz page
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=myReplace(A1)
Thanks. It worked for my purpose.
 
Upvote 0
Hi, sorry - I just noticed a couple of typos in my code, you should use this instead.

Rich (BB code):
Function myReplace(s As String)
Dim v As Variant, i As Long
v = Split(s, " ")
For i = 0 To UBound(v)
    If v(i) = "Page" Then
        If i < UBound(v) Then
            If IsNumeric(v(i + 1)) Then v(i + 1) = "xyz"
        End If
    End If
Next i
myReplace = Join(v, " ")
End Function
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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