In Excel VBA, put two double-quote characters inside a string literal wherever you want the resulting text to contain one double quote. For example, the VBA expression "She said ""Hello.""" produces She said "Hello."; you can also insert a quote with Chr$(34).
Why VBA requires doubled quotes
VBA uses double quotes to mark the beginning and end of a string literal. In a line such as message = "Hello", the first quote opens the literal and the last one closes it. If you want a quote character to be part of the text, VBA needs a way to distinguish that character from the one that ends the literal. Its rule is to write two quote characters together inside the literal: "" represents one quote in the resulting string.
This is a rule for writing a string literal in VBA code, not a change to the text itself. When VBA evaluates a literal, it removes the extra quote used for escaping. Thus, the sequence """" in code represents a string containing a single double-quote character: the first and last quotes delimit the literal, while the two in the middle represent the character.
VBA does not use the backslash escape convention found in some programming languages. Writing \" does not make a quote safe in a VBA string; backslash is an ordinary character in VBA string literals. This difference is a common source of syntax errors for people moving between VBA and languages such as JavaScript, C#, or Python.
Include quotes with doubled quotation marks
Use doubled quotes for ordinary text because the code makes the intended output visible without extra string-building steps. In this complete example, the message box displays She said "Hello.". The outer quotes define the VBA string, and each pair inside the literal produces one quote in the message.
Sub ShowQuotedText()
Dim message As String
' Paired quotes inside the literal become single quote characters.
message = "She said ""Hello."""
MsgBox message, vbInformation, "Quoted text"
End Sub
Count from the outside in when a run of quotes looks confusing. In "She said ""Hello.""", the opening quote starts the string, each doubled pair adds a quote to the text, and the final quote closes the string. The three adjacent quote marks at the end are intentional: two make the closing quote in the displayed sentence, and the third ends the VBA literal.
For longer sentences, break the text into pieces or put the message on multiple lines of code with the concatenation operator &. This can make the boundary between the literal and its embedded quote easier to inspect. Do not replace the string delimiters with apostrophes: VBA uses double quotes to delimit strings, while an apostrophe begins a code comment outside a string.
Use Chr$(34) when it improves readability
Chr$(34) returns the character with code 34, which is a double quote in the character set used by VBA. It can be useful when constructing text piece by piece, especially when the literal would otherwise contain a hard-to-count run of quotes. It is not inherently more correct than doubling quotes; it is an alternative way to express the same character.
Sub ShowQuoteWithChr()
Dim message As String
' Chr$(34) supplies one double-quote character.
message = "She said " & Chr$(34) & "Hello" & Chr$(34) & "."
MsgBox message
End Sub
Both examples display the same sentence. For a short fixed phrase, doubled quotes are usually simpler. For code that repeatedly inserts a quote or combines values from several sources, Chr$(34) may make the boundaries clearer. The dollar sign requests a string return value; Chr(34) is also available, but Chr$ states the intended string type.
Before running any macro, save a copy of the workbook. Excel macros cannot be undone with Ctrl+Z, so a macro that changes cells may leave no simple undo route. The quote examples above display messages or assign a specific value, but the same precaution is sensible for any macro that edits a workbook.
Put quoted text into a worksheet cell
Once the string is built, assign it to a cell as you would any other text. The example below places The file is called "Report.xlsx". in cell A1 of the active worksheet. It does not require any special worksheet setting for the quote character.
Sub PutQuotedTextInA1()
Dim result As String
' The doubled pairs become quote characters in the cell value.
result = "The file is called ""Report.xlsx""."
ActiveSheet.Range("A1").Value = result
End Sub
If a macro is meant to work on a particular sheet rather than whichever sheet happens to be active, qualify the range with that worksheet instead of relying on ActiveSheet. That is a workbook-design choice, not a different rule for quotes. The quote characters are stored as ordinary characters in the cell value and can be searched, copied, or used in other VBA operations.
Quotes in worksheet formulas assigned by VBA
There are two parsing stages when VBA writes a formula that itself contains text in quotes. First, VBA parses its string literal, so each quote needed in the formula must be doubled in the VBA code. Then Excel parses the resulting formula using normal worksheet formula rules.
Sub WriteFormulaToB1()
Dim formulaText As String
' VBA turns doubled quotes into the quotes required by the formula.
formulaText = "=IF(A1=""Yes"",""Ready"",""Wait"")"
Range("B1").Formula = formulaText
End Sub
With the value Yes in A1, the formula entered in B1 evaluates to Ready; for another value it returns Wait. The formula stored in the cell is =IF(A1="Yes","Ready","Wait"). In this example, the VBA literal needs doubled quotes around Yes, Ready, and Wait because those quotes must survive VBA parsing and reach Excel.
You can also enter a formula directly in a worksheet without VBA. With no input cells required, enter this formula in A1: ="She said ""Hello""". The displayed result is She said "Hello". This is Excel formula syntax, not VBA syntax; the doubled-quote rule happens to be similar, but the formula is interpreted by Excel rather than by the VBA editor.
CSV, JSON, and other text formats
A quote embedded in a VBA string is not automatically escaped for every format that you might generate. Each format has its own rules. For example, a CSV field containing a comma or a double quote is normally enclosed in double quotes, and an embedded double quote is represented by two double quotes in the CSV output. The following example applies that CSV escaping rule to one field:
Sub MakeCsvField()
Dim fieldText As String
Dim csvField As String
' Double embedded quotes, then surround the field with quotes.
fieldText = "A, ""quoted"" value"
csvField = Chr$(34) & Replace(fieldText, Chr$(34), _
Chr$(34) & Chr$(34)) & Chr$(34)
Debug.Print csvField
End Sub
The Immediate window output is "A, ""quoted"" value". That is a CSV representation of the field, not the text that a person would normally want to see in a cell. A complete CSV file also has to handle separators, line breaks, and the format expected by its recipient. For files with complex data, use a suitable CSV-export approach rather than assuming that doubling quotes alone handles every case.
JSON uses a different escaping convention: quotes inside a JSON string are preceded by a backslash in the JSON text. VBA still uses doubled quotes in its own source literal. For example, a VBA literal that creates the JSON text {"name":"Ada"} can be written as "{""name"":""Ada""}". That fixed example illustrates the two layers. For arbitrary values, a JSON serializer should also escape backslashes and control characters correctly; simply doubling VBA source quotes does not make arbitrary data valid JSON.
Common mistakes and how to fix them
The most frequent problem is confusing the character used in the VBA source with the character wanted in the output. Inspect the result separately from the code: one doubled pair in a string literal should produce one quote in the string. If the VBA editor reports a syntax error, check that the literal has a closing delimiter and that every embedded quote is doubled.
- Using a backslash escape:
\"is not VBA’s way to include a quote. Use""inside a literal or concatenateChr$(34). - Adding too many quote marks: A quote pair inside a literal becomes one output character. Extra pairs therefore produce extra quote characters in the result, even if the code compiles.
- Mixing formula and VBA syntax: A formula assigned through VBA must first be valid as a VBA string. Double the quotes in the VBA source so Excel receives the intended formula.
- Using an apostrophe as a string delimiter: Apostrophes can appear inside a double-quoted string, as in
"don't", but they do not replace the required double-quote delimiters.
When debugging, assign the text to a variable and inspect it in the Locals or Watch window, or print it with Debug.Print and examine the Immediate window in the Visual Basic Editor. This separates a VBA parsing problem from a later problem with a formula, CSV file, or other output format. If the displayed result is wrong but the code runs, the issue is likely the number or placement of quote characters rather than a VBA setting.
Which method should you choose?
For a fixed phrase, use doubled quotes. It is the native, compact VBA convention and works well for message text, cell values, and formula strings. Use Chr$(34) when assembling a string dynamically or when repeated quote pairs make a line difficult to read. You can also assign the character once to a clearly named variable when a procedure uses it many times, though a short procedure often does not need that extra variable.
VBA applies the same string-literal rule in Excel for Microsoft 365 on Windows and in Excel for Mac: embedded literal quotes are doubled. The Visual Basic Editor and macro permissions can differ by platform, but those differences do not change how the string is written. Excel for the web does not run VBA macros, so use desktop Excel to run this code.
Frequently Asked Questions
Can I use \" to include a quote in VBA?
No. VBA does not use a backslash to escape a quote in a string literal. Write two double quotes inside the literal, or concatenate Chr$(34). A backslash typed in VBA is treated as an ordinary character.
How do I show quotation marks in a MsgBox?
Put each wanted quote inside the message string as a doubled pair. For example, a message literal written as "She said ""Hello.""" displays as She said "Hello.". The same string rules apply to the message-box title.
How do I put quotes around a value in a cell?
Build the text with doubled quotes in the VBA literal, then assign it to the cell’s Value property. For example, """ & valueText & """ combines a quote, the variable’s contents, and another quote. Make sure valueText is declared and contains the text you intend to write.
Is Chr$(34) better than doubled quotes?
Neither method is universally better. Doubled quotes are generally easiest for a short, fixed string. Chr$(34) can make dynamic string construction clearer or avoid counting a long run of adjacent quote marks.
Do single quotes work as string delimiters in VBA?
No. A VBA string literal must be delimited with double quotes. An apostrophe can be part of the string, such as "don't", but outside a string it starts a comment. Use doubled double quotes for an embedded double-quote character.
Related
- Excel Text to Columns Greyed Out? Causes and Fixes
- Excel Text to Columns: How to Split Data
- Excel Text to Columns: How to Enter a Delimiter
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Function Parameters: Defaults, ByRef, and MoreIn Excel VBA, a standard function parameter is a named input declared in the function’s parentheses, such as…
- Excel VBA Get Username: Windows or Excel User Name?In Excel VBA on Windows, use Environ$("USERNAME") to read the current Windows account name. Use Application.UserName instead if…
- VBA MsgBox OK Cancel: Button Options and VBA ExamplesYes. In VBA, use vbOKCancel as the buttons argument of MsgBox, then check whether the function returned vbOK…
- Screen Macro Recorder: How It Works and What to ChooseA screen macro recorder is a tool that records actions you perform on a computer—such as clicks, keystrokes,…
- Read a Text File with Excel VBA: Methods and ExamplesYes. In Excel for Microsoft 365 on Windows, VBA can read a plain-text file with the Open and…
- Excel VBA Frame With Scroll Bar: Setup and CodeTo make a scrollable area in Excel VBA, add a Frame to a UserForm, set its ScrollBars property…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.