Guide to Escape Characters in VBA
Contents
An Introduction to “Escape Characters”
During the runtime of a program you might need to display certain information or ask a question. Some examples are :
- While raising a support ticket, we get a ticket number after clicking on the submit button. Along with a number, we also get a message like “Keep this number for future reference. You will get a response within 6 days.”
- While filing income tax returns, we might get a message, “Your return has been filed successfully.” And then some additional instructions after that.
- When filing out a form, we might get instructions from a rejection/error message stating an expected password structure or mandatory fields.
These messages are either dynamic or hard-coded strings that are typed within double quotes.
Examples:
1. Your user id is “RAD00234”.
2. Maximum number of invalid attempts to login to this bank account.
Your account as been locked for security reasons.
Please contact your branch to get your account unlocked.
Strings are usually presented within double quotes in general. In the first example above, the user id value is within double quotes. This means that the first double quote — the one in front of the user id — would mark the end of that string. So can you guess how it is possible to display that entire sentence, double quotes and all?
In the second example, do you see that there are three sentences with each one starting in a new line?
It is an escape character that you display text like this. An escape character helps the character(s) that follow it be interpreted differently from their default behavior by the programming language.
Escape Character in VBA
In programming languages “\” — a backslash — is generally used as an escape character.
Example : \n
for newline, \t
for tab space.
But in VBA, we use a function named “chr()”. This in-built function returns the ASCII equivalent value of the parameter.
Syntax:
Chr(<number>)
Where number can be any number from 0 to 127 .
This function returns the corresponding ASCII value from the table below.
The ASCII Table
Dec | Hex | Binary | HTML | Char | Description |
0 | 0 | 0 | � | NUL | Null |
1 | 1 | 1 |  | SOH | Start of Header |
2 | 2 | 10 |  | STX | Start of Text |
3 | 3 | 11 |  | ETX | End of Text |
4 | 4 | 100 |  | EOT | End of Transmission |
5 | 5 | 101 |  | ENQ | Enquiry |
6 | 6 | 110 |  | ACK | Acknowledge |
7 | 7 | 111 |  | BEL | Bell |
8 | 8 | 1000 |  | BS | Backspace |
9 | 9 | 1001 | 	 | HT | Horizontal Tab |
10 | 0A | 1010 | | LF | Line Feed |
11 | 0B | 1011 |  | VT | Vertical Tab |
12 | 0C | 1100 |  | FF | Form Feed |
13 | 0D | 1101 | | CR | Carriage Return |
14 | 0E | 1110 |  | SO | Shift Out |
15 | 0F | 1111 |  | SI | Shift In |
16 | 10 | 10000 |  | DLE | Data Link Escape |
17 | 11 | 10001 |  | DC1 | Device Control 1 |
18 | 12 | 10010 |  | DC2 | Device Control 2 |
19 | 13 | 10011 |  | DC3 | Device Control 3 |
20 | 14 | 10100 |  | DC4 | Device Control 4 |
21 | 15 | 10101 |  | NAK | Negative Acknowledge |
22 | 16 | 10110 |  | SYN | Synchronize |
23 | 17 | 10111 |  | ETB | End of Transmission Block |
24 | 18 | 11000 |  | CAN | Cancel |
25 | 19 | 11001 |  | EM | End of Medium |
26 | 1A | 11010 |  | SUB | Substitute |
27 | 1B | 11011 |  | ESC | Escape |
28 | 1C | 11100 |  | FS | File Separator |
29 | 1D | 11101 |  | GS | Group Separator |
30 | 1E | 11110 |  | RS | Record Separator |
31 | 1F | 11111 |  | US | Unit Separator |
32 | 20 | 100000 |   | space | Space |
33 | 21 | 100001 | ! | ! | Exclamation mark |
34 | 22 | 100010 | " | “ | Double quote |
35 | 23 | 100011 | # | # | Number |
36 | 24 | 100100 | $ | $ | Dollar sign |
37 | 25 | 100101 | % | % | Percent |
38 | 26 | 100110 | & | & | Ampersand |
39 | 27 | 100111 | ' | ‘ | Single quote |
40 | 28 | 101000 | ( | ( | Left parenthesis |
41 | 29 | 101001 | ) | ) | Right parenthesis |
42 | 2A | 101010 | * | * | Asterisk |
43 | 2B | 101011 | + | + | Plus |
44 | 2C | 101100 | , | , | Comma |
45 | 2D | 101101 | - | – | Minus |
46 | 2E | 101110 | . | . | Period |
47 | 2F | 101111 | / | / | Slash |
48 | 30 | 110000 | 0 | 0 | Zero |
49 | 31 | 110001 | 1 | 1 | One |
50 | 32 | 110010 | 2 | 2 | Two |
51 | 33 | 110011 | 3 | 3 | Three |
52 | 34 | 110100 | 4 | 4 | Four |
53 | 35 | 110101 | 5 | 5 | Five |
54 | 36 | 110110 | 6 | 6 | Six |
55 | 37 | 110111 | 7 | 7 | Seven |
56 | 38 | 111000 | 8 | 8 | Eight |
57 | 39 | 111001 | 9 | 9 | Nine |
58 | 3A | 111010 | : | : | Colon |
59 | 3B | 111011 | ; | ; | Semicolon |
60 | 3C | 111100 | < | < | Less than |
61 | 3D | 111101 | = | = | Equality sign |
62 | 3E | 111110 | > | > | Greater than |
63 | 3F | 111111 | ? | ? | Question mark |
64 | 40 | 1000000 | @ | @ | At sign |
65 | 41 | 1000001 | A | A | Capital / Upper case A |
66 | 42 | 1000010 | B | B | Capital / Upper case B |
67 | 43 | 1000011 | C | C | Capital / Upper case C |
68 | 44 | 1000100 | D | D | Capital / Upper case D |
69 | 45 | 1000101 | E | E | Capital / Upper case E |
70 | 46 | 1000110 | F | F | Capital / Upper case F |
71 | 47 | 1000111 | G | G | Capital / Upper case G |
72 | 48 | 1001000 | H | H | Capital / Upper case H |
73 | 49 | 1001001 | I | I | Capital / Upper case I |
74 | 4A | 1001010 | J | J | Capital / Upper case J |
75 | 4B | 1001011 | K | K | Capital / Upper case K |
76 | 4C | 1001100 | L | L | Capital / Upper case L |
77 | 4D | 1001101 | M | M | Capital / Upper case M |
78 | 4E | 1001110 | N | N | Capital / Upper case N |
79 | 4F | 1001111 | O | O | Capital / Upper case O |
80 | 50 | 1010000 | P | P | Capital / Upper case P |
81 | 51 | 1010001 | Q | Q | Capital / Upper case Q |
82 | 52 | 1010010 | R | R | Capital / Upper case R |
83 | 53 | 1010011 | S | S | Capital / Upper case S |
84 | 54 | 1010100 | T | T | Capital / Upper case T |
85 | 55 | 1010101 | U | U | Capital / Upper case U |
86 | 56 | 1010110 | V | V | Capital / Upper case V |
87 | 57 | 1010111 | W | W | Capital / Upper case W |
88 | 58 | 1011000 | X | X | Capital / Upper case X |
89 | 59 | 1011001 | Y | Y | Capital / Upper case Y |
90 | 5A | 1011010 | Z | Z | Capital / Upper case Z |
91 | 5B | 1011011 | [ | [ | Left square bracket |
92 | 5C | 1011100 | \ | \ | Backslash |
93 | 5D | 1011101 | ] | ] | Right square bracket |
94 | 5E | 1011110 | ^ | ^ | Caret / circumflex |
95 | 5F | 1011111 | _ | _ | Underscore |
96 | 60 | 1100000 | ` | ` | Grave / accent |
97 | 61 | 1100001 | a | a | Lower case a |
98 | 62 | 1100010 | b | b | Lower case b |
99 | 63 | 1100011 | c | c | Lower case c |
100 | 64 | 1100100 | d | d | Lower case d |
101 | 65 | 1100101 | e | e | Lower case e |
102 | 66 | 1100110 | f | f | Lower case f |
103 | 67 | 1100111 | g | g | Lower case g |
104 | 68 | 1101000 | h | h | Lower case h |
105 | 69 | 1101001 | i | i | Lower case i |
106 | 6A | 1101010 | j | j | Lower case j |
107 | 6B | 1101011 | k | k | Lower case k |
108 | 6C | 1101100 | l | l | Lower case l |
109 | 6D | 1101101 | m | m | Lower case m |
110 | 6E | 1101110 | n | n | Lower case n |
111 | 6F | 1101111 | o | o | Lower case o |
112 | 70 | 1110000 | p | p | Lower case p |
113 | 71 | 1110001 | q | q | Lower case q |
114 | 72 | 1110010 | r | r | Lower case r |
115 | 73 | 1110011 | s | s | Lower case s |
116 | 74 | 1110100 | t | t | Lower case t |
117 | 75 | 1110101 | u | u | Lower case u |
118 | 76 | 1110110 | v | v | Lower case v |
119 | 77 | 1110111 | w | w | Lower case w |
120 | 78 | 1111000 | x | x | Lower case x |
121 | 79 | 1111001 | y | y | Lower case y |
122 | 7A | 1111010 | z | z | Lower case z |
123 | 7B | 1111011 | { | { | Left curly bracket |
124 | 7C | 1111100 | | | | | Vertical bar |
125 | 7D | 1111101 | } | } | Right curly bracket |
126 | 7E | 1111110 | ~ | ~ | Tilde |
127 | 7F | 1111111 |  | DEL | Delete |
Escaping a Double Quote
In order to display one double quote as a part of the string, we have to use double quotes twice consecutively. So you should use a double quote to escape a double quote.
Below, I also show you how to use the Chr()
function to output a line feed, the character “#”, and “%”.
Example:
Sub demo_escape() ' see the differences in output Debug.Print "I love to drink tea." Debug.Print "I love to drink ""tea""." Debug.Print "I live in England." Debug.Print "I live in ""England""." 'chr(10) is equivalent to a new line. two sentences are concatenated here. Debug.Print "I have got promoted. What about you?" Debug.Print "I have got promoted." & Chr(10) & "What about you?" Debug.Print Chr(35) & "Number" Debug.Print Chr(37) & "Percent" End Sub
Conclusion
An escape character is very helpful for displaying strings or messages the way we want. As an automation specialist, I have faced situations where some unknown characters need to be identified on a webpage. The built-in function Chr()
has very helpful for me in difficult situations like this.
I’ve also widely used this in my coding too, because of its robustness and reliability. VBA has certainly simplified the use of escape characters by introducing a function that uses the ASCII table. One last bonus tip — there is a function ASCII(<character>)
that you can use to return the ASCII number (Dec) from the table above.
One thought on “Guide to Escape Characters in VBA”