Excel (2007) VBA - .Formula 带引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10142448/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:56:30  来源:igfitidea点击:

Excel (2007) VBA - .Formula with quotes in it

excelexcel-vbaexcel-2007vba

提问by GeoffDS

I tried putting the following code into a program yesterday. VBA called an error. I assume it is because of the double quotes inside the formula. I googled and all results I found just gave the basic of putting formulas in, but none explained how to get around quotes inside.

我昨天尝试将以下代码放入程序中。VBA 调用了一个错误。我认为这是因为公式中的双引号。我用谷歌搜索,我发现的所有结果都给出了输入公式的基本知识,但没有人解释如何绕过里面的引号。

(there was a With statement before this, Pivot is a worksheet name)

(在此之前有一个 With 语句,Pivot 是一个工作表名称)

.Range("A2").Formula = "=IF(Pivot!A5="",A1,Pivot!A5)" 

Any help is much appreciated. Thanks!

任何帮助深表感谢。谢谢!

回答by Siddharth Rout

Whenever in doubt, record a macro if it allows :)

如有疑问,请在允许的情况下录制宏:)

Try this

尝试这个

.Range("A2").Formula = "=IF(Pivot!A5="""",A1,Pivot!A5)" 

回答by Marc

Use Chr(34)in place of a double-quote.

使用Chr(34)代替双引号的。

So in your case:

所以在你的情况下:

.Range("A2").Formula = "=IF(Pivot!A5=" & Chr(34) & Chr(34) & ",A1,Pivot!A5)"

回答by Greg

you might need to do this:

你可能需要这样做:

.Range("A2").Formula = "=IF(Pivot!A5="& """" & """" & ",A1,Pivot!A5)"