How to add double quotes in a string literal

Code example:

Dim a As String a = 1234,5678,9123 

I want to add literal double quotes to variable a

Expected Result:

 a = "1234,5678,9123" 

How do I format a string, so when I print it, does it have double quotes?

+7
source share
6 answers

If you want to include " in the line, put "" where you want the quote to be displayed. Therefore, your example should read ...

 a = """1234,5678,9123""" 
+22
source

Current answers are correct and valid, but sometimes the following can improve readability:

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

To make Chr $ (34) more readable:

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

This makes it easy to get the correct number of characters in all cases and is readable.

+7
source
 a = """1234,5678,9123""" 

or

 a= """" & a & """" 
+6
source


No need to add any complex functions, just use the following example to insert a double into a text field or into a text field.

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

or

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

I used the Chr $ method (34), for example:

 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 used the same path for python script as an Excel workbook to make it easier for users. I went crazy with quotes in 45 minutes before I found this topic. When paths exit active jobs (especially with spaces in Windows), I think this is the preferred method. Hope this helps someone else.

-2
source

All Articles