vba VBA中的引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13595741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quotation marks in VBA
提问by Paolo Bernasconi
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.
在我当前的 VBA 代码中,我有一个查询,我用它在我的Chr(34)
一些变量之间加上引号。
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:
我的代码对于一个人来说很混乱,对于不熟悉 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.
提前致谢。
回答by transistor1
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"
回答by Grady Patterson
I took a page from MS (the old vbCRLF
) a while back, and just define any "tricky" characters I'll need as a string at the top of my code ...
不久前,我从 MS(旧的vbCRLF
)那里拿了一页,然后将我需要的任何“棘手”字符定义为代码顶部的字符串......
Dim vbDblQuote As String
vbDblQuote = Chr(34)
Now you can just use that pseudo-constant as you build strings ...
现在您可以在构建字符串时使用该伪常量......
strMyString = "Just another string " & vbDblQuote & "with quotes" & vbDblQuote & "!"
This makes code more readable, and also helps avoid "miscounted quote errors"
这使代码更具可读性,也有助于避免“错误计数的引用错误”
回答by John
I have found the same behavior if I output to a file using the Write command. Any double quotes get repeated.
如果我使用 Write 命令输出到文件,我发现了相同的行为。任何双引号都会重复。
However, if you construct the string and then output to file using the Print command, this does not happen and all works as expected.
但是,如果您构造字符串然后使用 Print 命令输出到文件,则不会发生这种情况,并且一切都按预期工作。