Gopal
Gopal

Reputation: 11972

How to add double quotes in a string literal

Example code:

Dim a As String
a = 1234,5678,9123

I want to add literal double quotes to the variable a

Expected Output:

a = "1234,5678,9123"

How do I format the string so when I print it, it has double quotes around it?

Upvotes: 14

Views: 103020

Answers (7)

Codemaker2015
Codemaker2015

Reputation: 1

You just use Chr$(34) to insert a double quotes.

Eg:

Dim i as String

i = Chr$(34) & "Hello World" & Chr$(34) 'Shows "Hello World"

Upvotes: 1

Pritam Zope
Pritam Zope

Reputation: 69


No need to add any kind of complicated functions just use following example to insert double in text box or rich text box.

Dim dquot=""""
TextBox1.AppendText("Hello " &dquot &"How are you ?" &quot)

or

Dim dquot=""""
RichTextBox1.AppendText("Hello " &dquot &"How are you ?" &quot)

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65264

a = """1234,5678,9123"""

or

a= """" & a & """"

Upvotes: 8

MikeF
MikeF

Reputation: 1008

To make Chr$(34) more readable:

Dim quote as string
quote = Chr$(34)
a = quote & "1234,5678,9123" & quote

This makes it easier to get the correct number of " symbols everywhere and is readable.

Upvotes: 11

TimWaters
TimWaters

Reputation: 27

I used the Chr$(34) method, like this:

Sub RunPython()
    Dim scriptName As String
    Dim stAppName As String
    scriptName = ActiveWorkbook.Path & "\aprPlotter.py"
    stAppName = "python.exe " & Chr$(34) & scriptName & Chr$(34)
    Debug.Print stAppName
    Call Shell(stAppName, vbHide)
End Sub

I was using the same path for the python script as the Excel Workbook, to keep it easier for the users. I monkeyed with the quotes for 45 minutes before I found this thread. When the paths are sourced from active working locations (especially ones with spaces in Windows), I think this is a preferred method. Hopefully, this will help someone else.

Upvotes: -2

Matt Wilko
Matt Wilko

Reputation: 27322

The current answers are correct and valid but sometimes the following can improve readability:

a = Chr$(34) & "1234,5678,9123" & Chr$(34)

Upvotes: 18

Brian Hooper
Brian Hooper

Reputation: 22044

If you want to include " in a string, supply "" where you want the quote to appear. So your example should read...

a = """1234,5678,9123"""

Upvotes: 28

Related Questions