Smart Systems for Home

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

Excel VBA: How to Put Quotes in a String

· 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 "" inside the quoted text. For example, MsgBox "He said ""Hello""" displays He said "Hello". That is the whole trick. VBA uses the double quote as its string delimiter, so a literal quote is escaped by repeating it. The same rule applies in cell formulas built from VBA, in SQL strings, and in file paths that contain quote characters.

What You'll Need

  • Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer). Menu labels can differ slightly by Excel version and region.
  • A standard module: press Alt+F11 to open the VBA editor, then Insert > Module.
  • Macro security set to allow macros, or the workbook saved as .xlsm and opened with macros enabled.
  • Basic familiarity with MsgBox or Debug.Print to see output.

Step-by-Step

  1. Open the VBA editor and add a module. Press Alt+F11, then choose Insert > Module. All code below goes in that module.
  2. Write a string with no quotes first. In the Immediate window (Ctrl+G) type ? "Hello" and press Enter. VBA returns Hello. The outer quotes are delimiters, not part of the text.
  3. Double the inner quote. Type ? "He said ""Hello""" and press Enter. The result is He said "Hello". Count the quotes: two to open, two for the literal quote, two for the closing quote. Every literal quote costs two characters in the source.
  4. Use Chr(34) when doubling gets unreadable. Chr(34) returns a double quote character. ? "He said " & Chr(34) & "Hello" & Chr(34) produces the same output. This is easier to read when a string contains many quotes, such as JSON or HTML fragments.
  5. Build a formula that contains quotes. To write =IF(A1="Yes",1,0) into a cell, use Range("B1").Formula = "=IF(A1=""Yes"",1,0)". The doubled quotes become single quotes in the formula. Test by reading the cell back.
  6. Handle single quotes separately. An apostrophe inside a double-quoted string needs no escaping: ? "It's fine" works. But if you wrap a value in single quotes for SQL, you must double any apostrophe in the data: Replace(value, "'", "''").
  7. Use a helper constant for readability. At the top of the module write Const Q As String = """". That constant holds one double quote. Then ? "He said " & Q & "Hello" & Q is clean and easy to scan.
  8. Check the output before shipping. Use Debug.Print to write the finished string to the Immediate window. If the quotes look wrong there, they will look wrong in the cell, message box or query.

Common Mistakes

  • Using a single backslash to escape. VBA does not use \". Backslashes are literal characters. Only doubling works.
  • Forgetting the closing delimiter. "He said ""Hello"" is missing the final quote pair and causes a compile error. Count pairs: open, literal, close.
  • Mixing up single and double quotes. Single quotes start a comment in VBA, not a string. 'Hello' is a comment, not text.
  • Assuming Excel formula quotes and VBA quotes are the same. In a cell formula you type one quote pair; in VBA source you type two. When VBA writes the formula, the doubled pair collapses to one.
  • Forgetting to double apostrophes in SQL. A name like O'Brien breaks a query built with single-quoted values unless the apostrophe is doubled or a parameter is used.
  • Editing the wrong module. Code in a sheet module or ThisWorkbook may not run as expected. Keep general helpers in a standard module.

Tips

  • Use Chr(34) or a Const Q constant for strings with three or more quotes. Readability matters more than saving keystrokes.
  • For long text, build it in pieces and join with &. Each piece is easier to verify than one giant line.
  • When generating JSON, escape backslashes and quotes together. A quote inside a JSON string must be \" in the final text, which in VBA source means """" or Chr(34) plus a backslash.
  • Use Replace to sanitize user input before inserting it into formulas or SQL. Doubling quotes is safer than trusting the source.
  • Keep a scratch module with test lines using Debug.Print. Run them after any change to confirm the output.

Troubleshooting

Compile error: Expected: end of statement. A quote is unbalanced. Count the quote characters on the line; the total should be even, and every literal quote should appear as a pair.

The message box shows extra quotes. You doubled a quote that did not need doubling, or you used Chr(34) inside text that was already quoted. Print the string to the Immediate window to see exactly what VBA holds.

The formula in the cell is wrong. Read the cell's .Formula property back in the Immediate window. Compare it character by character with what you intended. A missing doubled pair is the usual cause.

SQL query fails on an apostrophe. Replace single quotes in the value with two single quotes before concatenating, or switch to a parameterized query through ADO. Parameterized queries avoid the problem entirely.

Nothing runs at all. Check that macros are enabled, the file is saved as .xlsm, and the code is in a standard module. If the workbook came from another source, Windows may block it; open the file properties and unblock it only if you trust the source.

Output looks right in the Immediate window but wrong in the cell. The cell may be formatted as text or the formula may be stored as a string. Set .Formula rather than .Value when you want Excel to evaluate it.

Frequently Asked Questions

How do I include a double quote in a VBA string?

Type two double quotes where you want one. "Say ""hi""" produces Say "hi". Alternatively use Chr(34).

Can I use a backslash to escape quotes in VBA?

No. VBA has no backslash escape. A backslash is an ordinary character. Only doubling the quote works.

Why does my formula show double quotes in the cell?

You likely doubled quotes that were already inside a quoted VBA string, so the extra pair became literal text. Build the formula string, print it, then assign it to .Formula.

Do single quotes need escaping in VBA?

Not inside a double-quoted string. They do need doubling when you wrap values in single quotes for SQL.

What is the cleanest way to write many quotes?

Declare Const Q As String = """" and concatenate Q where a quote is needed. It is easier to read than long runs of doubled quotes.

Does this work in Excel for Mac?

Yes. The VBA editor and string syntax are the same. Menu locations and keyboard shortcuts can differ slightly by version and region.