I have this code:
a = "xyz" g = "abcd " & a
After running it, the value of g
is abcd xyz
.
However, I want quotes around the value of a
in g
. After running the code, g
should be abcd "xyz"
instead.
How can I accomplish this?
When you enter a single quotation mark, you must enter a second – VBA wants to see a pair of them. If you omit the quotation marks, VBA will interpret Harkins as a variable and then return an error when it doesn't find it (assuming of course that you haven't actually declared a variable named Harkins.)
You begin the string with a single double-quote, indicating the start of a string literal. Then you want to have an embedded double quote character, so you use two of them. This is where the escaping starts: you escape the double quote character with another double quote character.
The VBScript script concatenation operator is an ampersand "&" and occurs in between the two strings to be joined. This example will join a total of 4 strings to form a super string.
In two of the arguments, you have opening quotation marks embedded in the string (that's what the Chr(34) represents), but no closing quote. While that's legal VBScript code, it probably won't work the way you intended.
You can escape by doubling the quotes
g="abcd """ & a & """"
or write an explicit chr()
call
g="abcd " & chr(34) & a & chr(34)
You have to use double double quotes to escape the double quotes (lol):
g = "abcd """ & a & """"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With