Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quotation marks in VBA

In my current VBA code, I have a query in which I am using Chr(34) to put quotation marks between some of my variables.

I was wondering what alternatives exist to this. This is a simple question, i know, but I haven't had any success with repeating quotation marks like this

" & variable string here & "

My code is messy for one and not understandable for people who are not familiar with VBA:

comboService = Chr(34) & Me.Combo8.Value & Chr(34)

Also, this hasn't worked:

comboService = """" & Me.Combo8.Value & """"

Can you perhaps tell me why?

Thanks in advance.

like image 576
Paolo Bernasconi Avatar asked Nov 28 '12 00:11

Paolo Bernasconi


People also ask

What is double quotes in VBA?

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. 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.

How do I add single quotes in Excel VBA?

When you enter a single quotation mark, you must enter a second – VBA wants to see a pair of them. If you omit the quotation marks, VBA will interpret Harkins as a variable and then return an error when it doesn't find it (assuming of course that you haven't actually declared a variable named Harkins.)

How do I put quotes into text in Excel?

Use "CHAR(34)" within formulas where you need to output quotation marks. For example, to add quotes around the text in cell A1, you would type "=CHAR(34)&A1&CHAR(34)" in an empty cell.


1 Answers

This:

comboService = """ & Me.Combo8.Value & """

is what you posted, but you need to add an extra quotation mark in order to add a literal quotation mark:

comboService = """" & Me.Combo8.Value & """"

Double-quotes within a string are what you are looking for.

aVar = "This: "" is a literal quotation mark"
like image 137
transistor1 Avatar answered Oct 15 '22 00:10

transistor1