VBA Find and replace if starts with and ends with

Klturi421

Board Regular
Joined
Aug 31, 2014
Messages
52
I have a very unique document that I am working to format that includes a string of text such as ${e://file/_X1236}. and each time a similar string appears, the number is different. For example there might be ${e://file/_X9561}. There are easily 200+ entries each with different numbers otherwise I would call each one individually.

Essentially, I am looking to have a script that can find the string that starts with ${ and ends with }. (period included). Once found, the script should replace it with "Global Question:".

For example:
${e://file/_X1236}. How are you today? => Global Question: How are you today?
${e://file/_X9561}. What is your favorite color? => Global Question: What is your favorite color?


I've seen plenty of scripts that can find the start or the end but nothing so far that does both.

Any ideas?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
With such a small data range, looping should be sufficient.
Code:
Sub Maybe()
Dim c As Range
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If Left(c, 2) = "${" And Mid(c, 18, 2) = "}." Then c.Value = "Global Question: " & Mid(c, 21, 256)
    Next c
End Sub
 
Upvote 0
i don't know where your "global question" has to come, so a guess.
VBA Code:
Sub test()
     s = "I have a very unique document that I am working to format that includes a string of text such as ${e://file/_X1236}. and each time a similar string appears, the number is different. For example there might be ${e://file/_X9561}. There are easily 200+ entries each with different numbers otherwise I would call each one individually. Essentially, I am looking to have a script that can find the string that starts with ${ and ends with }. (period included). Once found, the script should replace it with Global Question: For example: ${e://file/_X1236}. How are you today? => Global Question: How are you today? ${e://file/_X9561}. What is your favorite color? => Global Question: What is your favorite color?"

     sp = Split(Replace(Replace(s, "${", "|" & "${"), "}", "}." & "|"), "|")     'add a unused character just in front and behind that special text
     For i = 0 To UBound(sp)                                    'loop through the parts
          If Left(sp(i), 2) = "${" And Right(sp(i), 2) = "}." Then sp(i) = sp(i) & " Global Question : "     'if starts and ends with the "${" and "}.", then add "Global Question"
     Next

     s1 = Join(sp, "")                                          'join the parts

     Range("A1") = s                                            'original string
     Range("A2") = s1                                           'modified string
     'Range("A3") = Join(sp, vbLf)

End Sub
 
Upvote 0
I think this should work...
VBA Code:
Sub TryThis()
  Columns("A").Replace "${*}.", "Global Question:", xlPart, , , , False, False
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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