在VBA中的字符串中放置双引号 [英] Placing Double Quotes Within a String in VBA

查看:905
本文介绍了在VBA中的字符串中放置双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何在VBA中的String中放置双引号(),我知道我可以使用 char(34)函数,我也明白,另一种方法是使用4个双引号:,所有这一切来自以前的SO文章: / p>



但是,如果我尝试在String的末尾调整第二组双引号( +)从3到4,Visual Studio自动将其调整为5.没有办法我最终只能有4个引号。这是消息框中生成的字符串:





消息框中的字符串不是我希望拥有的实际输出,它们纯粹用于实验目的。然而,我注意到的是,显然需要在VBA中的String中允许的引号数量。有谁知道这个要求是什么?为什么IDE强制在第二个String中插入一个额外的引号?有人可以在两种情况下解释实际的String内容和格式化引用之间的差异吗?



一如以往,任何对此的帮助将不胜感激: )

解决方案

一般规则如下。



双引号(DQ)宣布字符串的开头。之后,一些DQ宣布字符串的结尾。然而,如果DQ前面是DQ,则它将被转义。转义意味着它是字符串的一个字符部分,而不是分隔符。



简单地说,当您有连续双倍的偶数一个字符串中的引号,说 2n ,这意味着有n个转义的双引号。当数字 时,说 2n + 1 ,你有n个转义的DQ和一个分隔符。 >

示例

 + iterator.Value +
'delimiter+ iterator.Value +delimiter
'^ escaped ^ escaped

+ iterator.Value +
'delimiter+ iterator .Value +'(缺少封闭的分隔符)
'^ escaped ^^都被转义了。

在后一种情况下,最后一个分隔符缺失,为此,VS为您插入,而您得到5个DQ。



最后的具体情况(只有4个DQ),第一个和最后一个是分隔符,里面有一个逃逸的DQ。这相当于 chr(34)


I'm having a hard time understanding how to place a double quote (") within a String in VBA. I know that I can easily do this using the char(34) function. I also understand that another way of doing this is to use 4 double quotes: """". All of this comes from a previous SO post:

How do I put double quotes in a string in vba?

However, my question is.... Why are 4 quotes needed? Do the first two act as the escape, the third is the quote itself, and the fourth is the terminating quote? Or does it work in a different way? I haven't been able to find a concrete answer as to how VBA treats these double quotes.

I've also noticed that if I try adding or removing the number of double quotes within a String, Visual Studio will dynamically add or remove double quotes. For example, I initially had the following String:

data = TGName + """ + iterator.Value + """

...which produces the following within a message box:

However, if I try adjusting the second set of double quotes at the end of the String (+ """) from 3 to 4, Visual Studio automatically adjusts this to 5. There's no way for me to only have 4 quotes at the end. This is the resulting String within a message box:

The Strings within the message boxes aren't the actual output that I'm hoping to have, they're purely for experimental purposes. However, what I've noticed is that there clearly is a requirement for the number of quotes that are allowed within a String in VBA. Does anyone know what that requirement is? Why is the IDE forcefully inserting an additional quote in the second String? Can someone explain the differences between the actual String contents and the formatting quotes within both cases that I've described?

As always, any assistance on this would be greatly appreciated :)

解决方案

The general rule is as follows.

The first double-quote (DQ) announces the beginning of a string. Afterwards, some DQ announces the end of the string. However, if a DQ is preceded by a DQ, it is "escaped". Escaped means it is a character part of the string, not a delimiter.

Simply put, when you have any even number of consecutive double-quotes inside a string, say 2n, this means there are n escaped double-quotes. When the number is odd, say 2n+1, you have n escaped DQs and a delimiter.

Examples

  """ + iterator.Value + """
' delimiter " + iterator.Value + " delimiter
'           ^ escaped            ^ escaped

  """ + iterator.Value + """"
' delimiter " + iterator.Value + ""  ' (missing enclosing delimiter) 
'           ^ escaped            ^^ both escaped.

In this latter case the last delimiter is missing, For this reason VS inserted it for you, and you got 5 DQs.

Finally the particular case """" (just 4 DQs), the first and last are delimiters, and inside there's one escaped DQ. This is equivalent to chr(34).

这篇关于在VBA中的字符串中放置双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆