Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: How to Use a String in Double Quotes

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To put a double quote inside a VBA string, double it: write """" in code to produce one " in the string. For example, MsgBox """Hello""" displays "Hello" with quotes. You can also use the built-in constant vbQuote, which equals one double-quote character, so MsgBox vbQuote & "Hello" & vbQuote gives the same result. That is the whole trick: VBA uses the double quote to start and end a string, so a literal quote inside the string must be escaped by writing two of them.

What You'll Need

  • Microsoft Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
  • A workbook saved as .xlsm if you want to keep macros.
  • Basic familiarity with the Visual Basic Editor (Alt+F11).

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 in Excel. If the Developer tab is visible, you can also click Developer > Visual Basic. The exact label may differ slightly by Excel version or region.
  2. Insert a module. In the VBE, choose Insert > Module. A blank code window appears.
  3. Write a simple test procedure. Type the following (the editor will colour the strings):
    Sub ShowQuote()
        MsgBox """Hello"""
    End Sub
    Notice the four quote marks at the start and end of the string literal. The outer pair begins and ends the string; the inner pair produces one literal double quote.
  4. Run it. Press F5 with the cursor inside the procedure. A message box should show "Hello" including the quote marks.
  5. Use vbQuote for readability. Replace the literal with:
    MsgBox vbQuote & "Hello" & vbQuote
    This does the same thing and is easier to read when you have many quotes.
  6. Build a string with quotes and variables. For example:
    Dim sName As String
    sName = "Alice"
    MsgBox "The name is """ & sName & """."
    This displays: The name is "Alice". The pattern is: close the string, add a doubled quote, concatenate the variable, then add another doubled quote and reopen the string.
  7. Write quotes to a cell. Use:
    Range("A1").Value = """Quoted text"""
    Cell A1 will contain "Quoted text" with the quote characters. If you only want to display quotes visually without storing them, use a custom number format instead, but that is a separate technique.
  8. Test with Debug.Print. Instead of MsgBox, use Debug.Print """Test""" and press Ctrl+G to open the Immediate Window. This is faster for checking exactly what characters are in the string.

Common Mistakes

  • Using a single backslash escape. VBA does not use \" like many other languages. Writing "\"Hello\"" will cause a compile error or unexpected output. Always double the quote.
  • Losing count of quotes. Four quotes in a row ("""") is correct for one literal quote. Three quotes (""") is a syntax error because it leaves the string unclosed. Count in pairs.
  • Forgetting to close the string after concatenation. When you write "Text """ & var, the string must be reopened with a quote before the ampersand if more text follows. A common error is "Text """ & var & """ which is actually correct; the mistake is omitting the final opening quote before the closing quote.
  • Assuming vbQuote works in all Office apps. It is a VBA constant and works in Excel VBA, but if you copy code to another environment, check that the constant is available.
  • Using smart quotes. If you paste code from a word processor, curly quotes (“ ”) may replace straight quotes and cause errors. Type quotes directly in the VBE.

Tips

  • Use vbQuote when building strings with many quotes; it reduces counting errors.
  • Use Chr(34) as an alternative to vbQuote. Both produce a double-quote character. Chr(34) is useful when you need to build a quote from a character code.
  • For very complex strings, build them in parts with a helper function or use Replace to insert quotes after the fact. For example, write a placeholder like [Q] and then Replace(s, "[Q]", vbQuote).
  • Remember that the Immediate Window (Ctrl+G) shows strings without surrounding quotes, so a displayed quote is the actual character. Use Debug.Print "[" & s & "]" to see leading and trailing spaces.
  • If you are generating SQL or JSON, double quotes inside strings often need escaping for the target syntax as well. VBA's doubling only handles VBA's own string literals; the resulting string may still need further escaping for the destination format.

Troubleshooting

If you get a compile error "Expected: end of statement", check that every string literal has an even number of quote marks and that you have not left a string open. The VBE highlights the offending line. If the message box shows extra quotes, count the quotes in your literal: each pair inside the string becomes one character. If vbQuote is not recognised, you may be in a non-VBA environment or have a reference issue; use Chr(34) instead. If the code runs but the output is wrong, use Debug.Print and the Immediate Window to inspect the exact characters, including spaces. If pasted code fails, retype the quotes manually to remove smart quotes. If a macro is disabled, check Trust Center settings (File > Options > Trust Center > Trust Center Settings > Macro Settings) and enable macros for the file, but only for workbooks you trust.

Frequently Asked Questions

How do I put a double quote inside a VBA string?

Write two double quotes for each literal quote: """" produces one ". Alternatively use vbQuote or Chr(34).

What is the difference between vbQuote and Chr(34)?

Both return the same character. vbQuote is a named constant for readability; Chr(34) is a function call that returns the character with code 34.

Why does my string show three quotes instead of one?

You likely have an odd number of quotes or an extra pair. Each pair inside a string literal becomes one literal quote. Count the quotes carefully and use Debug.Print to verify.

Can I use single quotes instead?

Yes, single quotes are ordinary characters in VBA strings and do not need escaping. However, they are not the same as double quotes if the receiving system requires double quotes.

How do I include a double quote in a cell formula from VBA?

Build the formula string with doubled quotes, for example Range("A1").Formula = "=""Quoted""". Test with Debug.Print first because formula strings are easy to miscount.

Does this work in Excel for Mac?

Yes, VBA string escaping with doubled quotes works the same in Excel for Mac. The VBE interface and shortcut keys may differ slightly.