Inability to add quotation mark around a variable by concatenation in VBA

M

Mark Stephens

Hi,

I am outputting some data for another programmer to parse.

He has requested that I add quotation marks around each variable, but when I attempt to do it VBA will have none of it, and wants me to insert 2 quotation marks or none!

Here's my code:

Dim bCount As Byte
For bBandNo = 1 To 9

If arrbBandValue(bBandNo) > 0 Then

bCount = bCount + 1
'Last one needs special treatment (no comma after and quotes at the end
If bCount = bBandCount Then

sBandValues = sBandValues & arrbBandValue(bBandNo) & ";" & bBandNo
Exit For

End If

sBandValues = sBandValues & arrbBandValue(bBandNo) & ";" & bBandNo & ","

End If

Next bBandNo

psBandValues = sBandValues


I tried two approaches the first being to add them at the end which would be easiest. I just amended the last line:

psBandValues = """ & sBandValues & """

However this destroys my variable returning instead of "MyVariableString" as I intend, instead it returns

The second to do it 'at source' which yielded the same result.

Maybe I am missing something (probably) but should it be so hard to simply add quotation marks around a variable???

Any help much appreciated, M.
 
M

Mark Stephens

Hi,



I am outputting some data for another programmer to parse.



He has requested that I add quotation marks around each variable, but when I attempt to do it VBA will have none of it, and wants me to insert 2 quotation marks or none!



Here's my code:



Dim bCount As Byte

For bBandNo = 1 To 9



If arrbBandValue(bBandNo) > 0 Then



bCount = bCount + 1

'Last one needs special treatment (no comma after and quotes at the end

If bCount = bBandCount Then



sBandValues = sBandValues & arrbBandValue(bBandNo) & ";" & bBandNo

Exit For



End If



sBandValues = sBandValues & arrbBandValue(bBandNo) & ";" & bBandNo & ","



End If



Next bBandNo



psBandValues = sBandValues





I tried two approaches the first being to add them at the end which would be easiest. I just amended the last line:



psBandValues = """ & sBandValues & """



However this destroys my variable returning instead of "MyVariableString" as I intend, instead it returns



The second to do it 'at source' which yielded the same result.



Maybe I am missing something (probably) but should it be so hard to simply add quotation marks around a variable???



Any help much appreciated, M.

Hey Garry, that worked like a dream thanks so much for the help, M.
 
M

Martin Brown

Hi,

I am outputting some data for another programmer to parse.

He has requested that I add quotation marks around each variable, but when I attempt to do it VBA will have none of it, and wants me to insert 2 quotation marks or none!

Here's my code:

Dim bCount As Byte
For bBandNo = 1 To 9

If arrbBandValue(bBandNo) > 0 Then

bCount = bCount + 1
'Last one needs special treatment (no comma after and quotes at the end
If bCount = bBandCount Then

sBandValues = sBandValues & arrbBandValue(bBandNo) & ";" & bBandNo
Exit For

End If

sBandValues = sBandValues & arrbBandValue(bBandNo) & ";" & bBandNo & ","

End If

Next bBandNo

psBandValues = sBandValues


I tried two approaches the first being to add them at the end which would be easiest. I just amended the last line:

psBandValues = """ & sBandValues & """

However this destroys my variable returning instead of "MyVariableString" as I intend, instead it returns

The second to do it 'at source' which yielded the same result.

Maybe I am missing something (probably) but should it be so hard to simply add quotation marks around a variable???

It isn't but you have to escape them in. So to add one " you need two.

"""" = " as a string

So

a = """" & "test" & """"
debug.print a

will print "test"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top