excelvba quotes in string

'Including double-quote characters in VBA string literals can be confusing.
'First, realize that VBA (unlike many languages) does not recognize the
'apostrophe as a quotation mark. In JavaScript, for example you can simply 
'do this:

'	var s = 'The " is called the double-quote character.'

'And since the single-quote character and the double-quote character
'are both recognized for quotation, there is no ambiguity for the compiler.

'But VBA only recognizes the double-quote character as a quotation mark. As
'a result, the following VBA would be ambiguous and WILL NOT compile:

'   s = "The " is called the double-quote character."

'The solution is to realize that the INTERNAL double-quote character must be 
'escaped by another double-quote character:

s = "The "" is called the double-quote character."
MsgBox s    '<--displays:   The " is called the double-quote character.

'Of course, the entire string literal, like all string literals, must include 
'quotes both at the beginning and the end of the literal.

'Some examples:
MsgBox ""				  '<--displays:         (Nothing... an empty string)
MsgBox " "" "             '<--displays:  "      (1 quote w/1 space before and after)
MsgBox " """              '<--displays:  "      (1 quote w/1 space before)
MsgBox """ "              '<--displays: "       (1 quote w/1 space after)
MsgBox """"               '<--displays: "       (1 quote)
MsgBox """"""             '<--displays: "" 
MsgBox """"""""           '<--displays: """
MsgBox """Quoted text"""  '<--displays: "Quoted text"

'Remeber that the very first quote and the very last are required to denote
'the entire string literal. All of the INTERNAL quotes are escaped (doubled-up).

'-------------------------------------------------------------------------------

'Another method is to break the text into multiple string literals and
'concatenate:

MsgBox """" & "Quoted text" & """"        		'<--displays: "Quoted text"

'Or using a constant...

Const Q As String = """"
MsgBox Q & "Quoted text" & Q  			  		'<--displays: "Quoted text"

'-------------------------------------------------------------------------------

'Another method is to use ASCII character 34:

MsgBox Chr(34) & "Quoted text" & Chr(34)  		'<--displays: "Quoted text"

'Or...

q = Chr(34)
MsgBox q & "Quoted text" & q  			  		'<--displays: "Quoted text"

'-------------------------------------------------------------------------------

'Another method is to use substitution:

MsgBox Replace("/qQuoted text/q", "/q", """")	'<--displays: "Quoted text"

'The substitution method can really pay off if there are a lot of internal
'quotation marks that need to be included in the string literal. We are 
'simply replacing embedded /q with actual double-quote characters.
'
'There is nothing special about /q. You could use a different code, but be
'sure to pick something unique so that you do not unintentionally replace
'inappropriate text.
'
'
'




Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
excel vba use double quotes in string how to add double quotes in string vba vba replace double quotes in string vba put double quote in string quotes in formula vba use of quotes vba add quotes to string excel how to put a double quote in a string in vba vba extract quotes from string vba add quotes around value add double quotes in excel vba vba write double quotes literal quotes in string vba how to use literal quotes vba vba insert quotes in string vba double quotes in string vba quote in string vba value to string excel vba double quotes vba double quote in string vba double apostrophe vba string with double quotes excel vba add in string vba double quote vba character quotation marks excel vba insert quotation in string visual basic quotes inside string vba include quotes in string excel VBA char for open quotes vba escape double quote vba print double quote vba escape quotation mark vba inverted commas in string vba insert double quotes into string excel vba add inverted comma to range put quotes in a string in excel vba put quotes in a string in vba put double quotes in a string in vba put double quotes in a string in excel-vba put double quotes in a string in excelvba put double quotes in a string in excel vba put double quotes in a string in xlvba put double quotes in a string in xl vba How do I put double quotes in a string in xl vba How do I put double quotes in a string in xlvba How do I put double quotes in a string in excel vba How do I put double quotes in a string in excelvba How do I put double quotes in a string in vba vba quotes in string excelvba quotes in string xlvba quotes in string xl-vba quotes in string excel-vba quotes in string excel-vba quotes in string literal xl-vba quotes in string literal xlvba quotes in string literal xl vba quotes in string literal excel vba quotes in string literal excelvba quotes in string literal vba quotes in string literal vba double quotes in string literal xl vba double quotes in string literal xlvba double quotes in string literal excelvba double quotes in string literal excel vba double quotes in string literal
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source