Insert double quotes in a string in VBA

We can recognize a string in VBA when the value is inside double-quotes. Let’s how it works inside VBA editor.

Sub displayString()
    mystring = "123456"
    Selection.Value = mystring
End Sub

If you run this code, it will display the message inside a worksheet.

The message is displayed without double quotes and is treated as a number.

If you want to display string with double quotes, you just have to add additional quotes.

Sub displayString()
    mystring = """123456"""
    Selection.Value = mystring
End Sub

If you run this code, Excel will display a text with double-quotes.

If you want to add double double-quotes, you have to use 5 double-quotes for each side.

Sub displayString()
    mystring = """""123456"""""
    Selection.Value = mystring
End Sub

Run the code, to get the result.

The more practical way to use double-quotes inside a string is to use it for names.

Sub displayString()
    mystring = "Dwayne ""The Rock"" Johnson"
    Selection.Value = mystring
End Sub

This code will generate the following result:

Posted in vba