I have a section of code that writes to the header section of the page. The problem is that the string that is written to the header occasionally contains a ‘&’ symbol. This interferes with the code because VBA automatically views & and the following character as a piece of code, even if its part of the string.
ActiveSheet.PageSetup.CenterHeader = "&B&14 Cool New Header for &16 &I" & vbNewLine & aString
Everything works great when aString has no ‘&’ symbol in it. I get a bold, size 14 top line followed by the size 16, italicized aString text on a second line.
In the event that aString is something like ‘B&S Company of Greatness’ the header will come out with “Company of Greatness” stricken out because of the ‘&S’ contained within the string.
How can I get around this? I’d do a search in string for ‘&S and compensate manually by inserting another &S to cancel it out but &S might not be the only occurance of the ‘&’ symbol that occurs.
What’s the best way to get around this? Would properly diming things help by telling excel to read aString ONLY as a string value and to not apply its contents improperly?
You need to escape the ampersand by adding a second ampersand to each instance in the string, like this: