vba Excel转义单引号和双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17430938/
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
Excel Escaping Single and double quotes
提问by Charles Bernardes
I have a formula in each cell in excel where I need to edit. But I am having a hard time escaping the single quotes and double quotes using VBA code.
我在需要编辑的 excel 中的每个单元格中都有一个公式。但是我很难使用 VBA 代码转义单引号和双引号。
This is an example:
这是一个例子:
=+'F-222Alloc'!N2516+'F-222Alloc'!N2526
I need it to look like this
我需要它看起来像这样
=+INDIRECT("'"&N14&"'!N2511")+INDIRECT("'"&N14&"'!N2526")
How do I use the REPLACE function properly?
如何正确使用 REPLACE 功能?
回答by Floris
I find the easiest is to define a variable that contains just the double quote - then use it like any other string. Makes the code much more readable. Example:
我发现最简单的方法是定义一个只包含双引号的变量 - 然后像任何其他字符串一样使用它。使代码更具可读性。例子:
Dim dq As String, sq as string
dq = Chr(34) ' double quote as a variable
sq = Chr(39) ' apostrophe or single quote as variable
Dim sourceString As String
sourceString = "hello"
msgbox sq + sourceString + "! " + dq + "you" + dq + sq
With these two variables you can create any string you want - after that, replacing what you want with something else (that might contain a crazy sequence of "'"'"'"("!"'")
for all I care) becomes trivial.
使用这两个变量,您可以创建您想要的任何字符串 - 之后,用其他东西(可能包含"'"'"'"("!"'")
我所关心的疯狂序列)替换您想要的东西变得微不足道。
Some helpful rules can be found in this article
一些有用的规则可以在这篇文章中找到