14

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?

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
Gopal
  • 11,712
  • 52
  • 154
  • 229
  • possible duplicate of [Escape double quote in VB string](http://stackoverflow.com/questions/4835691/escape-double-quote-in-vb-string) – GSerg Dec 20 '11 at 11:31
  • IS this just in the VB sources? Or is there something more here? The obvious question is why can't you just type this? Can you provide more context -- is this code that you need to refactor? How many instances are we talking about here as well? Are all variables declared as "Dim a as string"? What other constraints are there? – Chris J Dec 20 '11 at 11:31
  • 1
    Possible duplicate of [Escape double quote in VB string](https://stackoverflow.com/questions/4835691/escape-double-quote-in-vb-string) – StayOnTarget Nov 21 '17 at 16:47

7 Answers7

28

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"""
Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
18

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

a = Chr$(34) & "1234,5678,9123" & Chr$(34)
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 2
    I didn't down-vote, but that is NOT more readable! Are all developers expected to know the entire ASCII character code set? Create a constant and assign it that value; then, simply use the constant – UnhandledExcepSean Dec 20 '11 at 12:39
  • 6
    Well I only said *can* - just to give another option to the OP. And I didn't say this was best practise. You can find the ASCII character set [here](http://www.asciitable.com/) - then you don't have to remember it. – Matt Wilko Dec 20 '11 at 12:55
  • 6
    +1 I see Chr(34) all over the place, not sure what the problem is. – JimmyPena Dec 20 '11 at 20:33
  • 3
    This is far easier to read because you don't have to make sure you are doubling up each quote. Plus the string is there in plain English. – surfasb Feb 03 '12 at 23:16
  • 1
    A few years later this still helps! + 1 – Jens Bergvall Sep 03 '14 at 12:18
  • 1
    `Chr$()` is also a function call so will be slower than a string literal or a constant. – Deanna Sep 04 '14 at 10:32
  • 1
    @Deanna - valid point but I suspect the difference would be negligible. – Matt Wilko Sep 04 '14 at 10:40
  • 2
    create a global constant: Public Const vbDoubleQuote As String = """" 'represents 1 double quote (") and use it like: a = vbDoubleQuote & "blabla" & vbDoubleQuote – gicalle Sep 11 '14 at 09:39
11

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.

MikeF
  • 764
  • 9
  • 26
7
a = """1234,5678,9123"""

or

a= """" & a & """"
Deanna
  • 23,876
  • 7
  • 71
  • 156
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • @MattWilko It does if `a` is typed as a string as per the OP. If however they use a numeric variable or literal in there, the `+` should be `&`, or the value passed to `CStr()`. – Deanna Sep 04 '14 at 11:05
  • @MattWilko Sorry :) I've updated the answer to use `&` anyway. – Deanna Sep 04 '14 at 12:19
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"
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0


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)
Qiu
  • 5,651
  • 10
  • 49
  • 56
-2

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.

TimWaters
  • 27
  • 3