Home » excel » vba – Excel macro to replace tags with variables

vba – Excel macro to replace tags with variables

Posted by: admin May 14, 2020 Leave a comment


I am using the below code to replace some text and tags in an exported qml file.

sTemp = Replace(sTemp, "<CONTENT>", "</CONTENT>")

But now i want to replace a tag with variable as below:

sTemp = Replace(sTemp, "<CONTENT ID="0">", "<CONTENT ID="0"><![CDATA[")

I appreciate any help.

How to&Answers:

Your Replace needs two string parts.
Their limiting doublequotes have to remain like they are shown here:

sTemp = Replace(sTemp, "...", "...")

If you exchange each … by your strings, double each of their doublequotes,
but leave the above limiting ones as they are.


Doublequote somewhere in the middle: <CONTENT ID="0">
                                     <CONTENT ID="0"><![CDATA[
sTemp = Replace(sTemp, "<CONTENT ID=""0"">", "<CONTENT ID=""0""><![CDATA[")

Doublequote at the end: ID="0"
sTemp = Replace(sTemp, "ID=""0""", """ID=1""") 

Double-Doublequote: =IF(A1="you got it","yeah :)","")
Range.Formula = "=IF(A1=""you got it"",""yeah :)"","""")"