Adding/updating text between a string or cell with text

WaqasTariq

Board Regular
Joined
Jun 26, 2012
Messages
58
Office Version
  1. 365
I have the following stored in a string (can be in a cell as well, if needed). Let's say s_String1(2,1) or cell A2:

This text is permanent

//Changeable-Text-Starts-Here

This text can be changed by macro

//Changeable-Text-Ends-Here

This is also permanent

I want to be able to remove /change / update the text "This text can be changed by macro" to something different that is stored in s_String2(2,1). I have been searching all over the forum but have not found an example of this being done before. Can anyone help with this?

This text is permanent

//Changeable-Text-Starts-Here

This text HAS been changed.

And I can add more lines to it.

//Changeable-Text-Ends-Here

This is also permanent
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try with this formula:

Book1
AB
1
2Some text
3
4This text is permanent //Changeable-Text-Starts-Here This text can be changed by macro //Changeable-Text-Ends-Here This is also permanentThis text is permanent //Changeable-Text-Starts-Here Some text //Changeable-Text-Ends-Here This is also permanent
Sheet2
Cell Formulas
RangeFormula
B4B4=SUBSTITUTE(A4,"This text can be changed by macro",A2)

___________________________________________________________
Or with macro:
VBA Code:
Sub Macro1()
  Range("B4").Formula = "=SUBSTITUTE(A4,""This text can be changed by macro"",A2)"
End Sub
 
Upvote 0
In VBA, there is the Replace function:

VBA Code:
Sub test()
Dim s As String
s = "This text is permanent //Changeable-Text-Starts-Here This text can be changed by macro //Changeable-Text-Ends-Here This is also permanent"
MsgBox Replace(s, "This text can be changed by macro", "New text")
End Sub
 
Upvote 0
Thank you guys, but the "This text can be changed by macro" is dynamic, while "//Changeable-Text-Starts-Here" and "//Changeable-Text-Ends-Here" are the only constants.
 
Upvote 0
Try this

=REPLACE(A4,SEARCH("//Changeable-Text-Starts-Here",A4)+LEN("//Changeable-Text-Starts-Here")+2,SEARCH("//Changeable-Text-Ends-Here",A4)-SEARCH("//Changeable-Text-Starts-Here",A4)-LEN("//Changeable-Text-Starts-Here")-4,A2)

1577475934739.png



With macro
VBA Code:
Sub Macro1a()
  Dim oText As String
  oText = "//Changeable-Text-Starts-Here"
  Range("B4").Formula = _
    Replace("=REPLACE(A4,SEARCH(""@"",A4)+LEN(""@"")+2,SEARCH(""//Changeable-Text-Ends-Here"",A4)-SEARCH(""@"",A4)-LEN(""@"")-4,A2)", "@", oText)
End Sub
 
Last edited:
Upvote 0
Try this

=REPLACE(A4,SEARCH("//Changeable-Text-Starts-Here",A4)+LEN("//Changeable-Text-Starts-Here")+2,SEARCH("//Changeable-Text-Ends-Here",A4)-SEARCH("//Changeable-Text-Starts-Here",A4)-LEN("//Changeable-Text-Starts-Here")-4,A2)


With macro
VBA Code:
Sub Macro1a()
  Dim oText As String
  oText = "//Changeable-Text-Starts-Here"
  Range("B4").Formula = _
    Replace("=REPLACE(RC[-1],SEARCH(""@"",RC[-1])+LEN(""@"")+2,SEARCH(""//Changeable-Text-Ends-Here"",RC[-1])-SEARCH(""@"",RC[-1])-LEN(""@"")-4,R[-2]C[-1])", "@", oText)
End Sub
Thank YOU! works perfectly.

Happy holidays!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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