Include an apostrophe in a VBA string
How to put an apostrophe in a VBA string: when a plain quote works, when to use Chr(39) or ChrW(8217), how to escape apostrophes in SQL, and how straight and curly apostrophes affect copying, searching and compatibility.
An apostrophe causes confusion in VBA because the same character also starts a comment. In practice, an apostrophe inside a double-quoted string literal is ordinary text and needs no escaping. The harder questions come later: whether to keep the straight apostrophe or convert it to a typographic one, and what happens when the string reaches SQL, an Excel cell or another application. This review covers each case and the tradeoffs.
The short answer
Type the apostrophe directly inside the double quotes. VBA delimits string literals with double quotes, so a single quote between them is part of the string and does not start a comment.
Dim msg As String
msg = "It's ready"
MsgBox msg
An apostrophe starts a comment only when it appears outside a string literal. Everything after it on that line is ignored by the compiler.
msg = "It's ready" ' this part is a comment
' msg = "This whole line is a comment"
The character that does need escaping inside a VBA string is the double quote, which you write as two double quotes in a row.
msg = "She said ""it's fine"""
' Result: She said "it's fine"
Is this transformation suitable for my intended use?
There are two common choices. You can keep the straight ASCII apostrophe (U+0027), or you can substitute the typographic right single quotation mark (U+2019), often called a curly or smart apostrophe.
- Keep the straight apostrophe for code, file names and paths, SQL, CSV and other data files, lookup keys, values that will be searched or compared, and anything exchanged with other systems. It is a single-byte ASCII character and the most widely compatible option.
- Use the typographic apostrophe only for display text where typographic polish matters, such as report headings or document text generated into Word, and only when every downstream step handles Unicode.
- When in doubt, store plain text with straight apostrophes and apply typographic formatting at the final presentation step.
The main benefit of the curly apostrophe is appearance in polished output. The decisive tradeoff is that it is a different character. Text that looks identical to a reader will not match in comparisons, lookups or searches, and some environments, including parts of the VBA editor itself, may not handle it reliably.
What does this transformation actually change?
Swapping a straight apostrophe for a curly one changes the stored character. It is not a font or styling effect. The string contains a different code point, so its length stays the same, but equality tests, InStr searches, Replace calls and dictionary or worksheet lookups treat the two versions as different text. By contrast, applying a font in Excel or Word changes only how characters are displayed and leaves the stored text unchanged.
Terms such as "reverse", "mirror" or "font" do not apply here. This is a one-for-one character substitution, and it affects neither reading order nor bidirectional display.
Ways to produce each character in code
| Approach | Example | Character produced | Notes |
| Literal in string | "It's" | U+0027 straight apostrophe | Simplest; no escaping needed |
| Chr(39) | "It" & Chr(39) & "s" | U+0027 straight apostrophe | Useful when building strings programmatically or to avoid editor confusion |
| ChrW(8217) | "It" & ChrW(8217) & "s" | U+2019 right single quotation mark | Independent of the system code page |
| Chr(146) | "It" & Chr(146) & "s" | U+2019 only on systems using the Windows-1252 code page | Code-page dependent; avoid in shared code |
| Pasting a curly apostrophe into the editor | "It’s" | Depends on the editor's code page | May be stored or shown as a different character on other systems |
The VBA editor works with the system's ANSI code page rather than full Unicode, so ChrW with the numeric code point is the reliable way to put a typographic apostrophe in source code. To convert in the other direction, normalize curly apostrophes to straight ones before comparing or storing:
Dim cleaned As String
cleaned = Replace(rawText, ChrW(8217), "'")
cleaned = Replace(cleaned, ChrW(8216), "'")
When the string goes somewhere else
SQL statements
An apostrophe that is harmless in VBA can break a SQL statement, because SQL commonly uses single quotes to delimit text. A value such as O'Brien ends the SQL literal early. Doubling the apostrophe inside the value is the standard escape:
Dim lastName As String
Dim sql As String
lastName = "O'Brien"
sql = "SELECT * FROM Customers WHERE LastName = '" & _
Replace(lastName, "'", "''") & "'"
Where the data access library supports them, parameterized queries are preferable to manual escaping. They avoid the quoting problem entirely and reduce SQL injection risk. Curly apostrophes do not end a SQL literal, but they will not match straight apostrophes already stored in the database.
Excel cell values and formulas
In Excel, a leading apostrophe in a cell entry is a prefix that marks the entry as text. It is not shown as part of the value. Writing Range("A1").Value = "'00123" stores the text 00123 and keeps the leading zeros. To display a literal leading apostrophe, the entry needs a second one. Apostrophes elsewhere in the text are stored normally.
When you write formulas through Range.Formula, the formula's own string literals use double quotes. Those are doubled in VBA source, and apostrophes inside them need no special treatment. One exception: sheet names containing spaces or apostrophes are wrapped in single quotes in references, and an apostrophe within such a sheet name is doubled.
Will the result remain readable and usable after copying or sharing?
Straight apostrophes survive copying between editors, email, spreadsheets and code with the fewest surprises. Curly characters often enter by accident: Word's AutoCorrect, many web pages and some chat tools convert straight quotes to typographic ones as you type or paste. Code copied from such sources into the VBA editor can fail. Curly double quotes are not recognized as string delimiters and cause syntax errors, and curly apostrophes inside strings silently change the data.
- Search and matching: a user searching for a straight apostrophe will not find text containing a curly one unless the search normalizes them.
- Editing: the two characters look alike in many fonts, which makes mismatches hard to spot by eye.
- Display: whether a given dialog, control or older application shows U+2019 correctly depends on its Unicode support and code page. This was not tested for any specific environment here, so verify in your actual target.
- Accessibility: no documentation or test results were supplied on how particular screen readers announce straight versus curly apostrophes. Do not assume behavior; test with the assistive technology your audience uses.
Provide or keep a plain-text equivalent with straight apostrophes whenever the text will be machine-processed, compared, imported into another system or edited by others. Reserve the typographic form for final, human-read output.
Evidence gaps
No subject-specific test results, version details or measurements were supplied for this review. The guidance above relies on general, documented VBA and Excel behavior. Rendering of Unicode apostrophes in specific dialogs, add-ins or assistive technologies is environment-dependent and has not been verified here.