简体   繁体   English

串联包含“和&字符的字符串

[英]Concatenating strings containing " and & characters

Problem 问题

I am working with some Excel slicers through VBA, and need to filter (a varying number of) items at the same time. 我正在通过VBA与一些Excel Slicer一起使用,并且需要同时过滤(数量不定的)项目。

The slicer commands include the """ and "&" characters, however, which obviously is problematic. 切片器命令包含“”和“&”字符,但是,这显然是有问题的。

I've tried treating the """ and "&" characters individually, eg "Some text" & """ & str1 & "&" & "more text" , as well as trying to bundle it all together, eg "Some text"" & str1 & "&more text" 我试过分别处理“”“和”&“字符,例如"Some text" & """ & str1 & "&" & "more text" ,并尝试将它们全部捆绑在一起,例如"Some text"" & str1 & "&more text"

Neither approach seems to work, generating a "Compile error: Expected: End of statement". 两种方法似乎都不起作用,生成了“编译错误:预期:语句结束”。


Question

Is there a better way to include the "&" and """ characters in a concatenation? 是否有更好的方式在串联中包含“&”和“””字符?


More Context 更多背景

This is the actual string layout I am trying to end up with, after all concatenation is complete: 在所有串联完成之后,这是我要最终使用的实际字符串布局:

"[Jobs].[ParentJob].&[Code1]", "[Jobs].[ParentJob].&[Code2]", "[Jobs].[ParentJob].&[Code3]"

Where Jobs and ParentJob remain the same (they are slicer categories and should be part of the string); JobsParentJob保持相同的地方(它们是切片器类别,应该是字符串的一部分); but Code1, Code2 etc... are variables which are to be concatenated one by one as a loop runs. 但是Code1,Code2等...是在循环运行时将一个接一个地连接的变量。

I'm confident running the loop and concatenating the right parts in the right places, I just can't find any resources anywhere that explain how to include " and & in concatenations when they are in a more complex format. 我有信心运行循环并将正确的部分连接在正确的位置,我只是在任何地方都找不到任何资源来解释如何以更复杂的格式包含“和”。

For a single " I normally use """" (4 doble quotes). 对于单个"我通常使用"""" (4个双引号)。

It's annoying but it works pretty good. 这很烦人,但是效果很好。 So for your test, if I do: 因此,对于您的测试,如果我这样做:

Debug.Print """" & "[Jobs].[ParentJob].&[Code1]" & """" & ", " & """" & _ "[Jobs].[ParentJob].&[Code2]" & """" & ", " & """" & "[Jobs].[ParentJob].&[Code3]" & """"

in the inmediate Window I get as output the text: 在中间窗口中,我得到如下输出:

"[Jobs].[ParentJob].&[Code1]", "[Jobs].[ParentJob].&[Code2]", "[Jobs].[ParentJob].&[Code3]"

Hope this helps 希望这可以帮助

UPDATE : For the char & there is no problem as long as it's between double quotes. 更新 :对于char & ,只要在双引号之间就没有问题。 Handle it as any other char text. 与其他任何char文本一样处理。 So doing "&" will return & and doing "[Jobs].[ParentJob].&[Code1]" , will return [Jobs].[ParentJob].&[Code1] 因此,执行"&"将返回&并执行"[Jobs].[ParentJob].&[Code1]" ,将返回[Jobs].[ParentJob].&[Code1]

UPDATE 2: Another option to make the code more readable would be assigning the """" to a string variable. 更新2:使代码更具可读性的另一种选择是将""""分配给字符串变量。 Something like this: 像这样:

Dim DblQuote As String

DblQuote = """"

Debug.Print DblQuote & "[Jobs].[ParentJob].&[Code1]" & DblQuote & ", " & DblQuote & _
"[Jobs].[ParentJob].&[Code2]" & DblQuote & ", " & DblQuote & "[Jobs].[ParentJob].&[Code3]" & DblQuote

This will output the text you need, but it's easier to work with it and make the code readable. 这将输出您需要的文本,但是使用它会更容易并使代码可读。

You have the option to use multiple quotes as suggested by @foxfire . 您可以选择使用@foxfire建议的多引号。 But I would also recommend using the CHR function in VBA. 但是我也建议在VBA中使用CHR函数

In VBA you can use Chr(38) & Chr(34) for "&" and (double quote - ") respectively. ie: 在VBA中,可以分别对“&”和(双引号-)使用Chr(38)Chr(34) 。即:

Chr(38) = & Chr(38) =&
and
Chr(34) = " Chr(34) =“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM