vba 带引号的公式中的预期语句结束

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

Expected End of Statement in formula with quotes

excelvbaexcel-2011

提问by PA_Commons

Sub FillNames()

Range("D2:D56").SpecialCells(xlCellTypeBlanks).Formula = _
    "=IF(AND(C>800,C<900),  "YES", "NO")"

End Sub

I get the compile error expected: end of statement. It seems to have issue with the quotes around the words YES and NO.

我得到预期的编译错误:语句结束。YES 和 NO 周围的引号似乎有问题。

If I remove the quotes around YES and NO and run the macro, it will run and print what is in the formula quotes into the cell. I don't understand then how the quotes cause a syntax error.

如果我删除 YES 和 NO 周围的引号并运行宏,它将运行并将公式引号中的内容打印到单元格中。我不明白引号是如何导致语法错误的。

I am running Excel 2011 for Mac, don't know if that has anything to do with it.

我正在为 Mac 运行 Excel 2011,不知道这是否与它有关。

回答by Tim Williams

Double-quotes in strings need to be escaped by doubling them up:

字符串中的双引号需要通过将它们加倍来转义:

Range("D2:D56").SpecialCells(xlCellTypeBlanks).Formula = _
                    "=IF(AND(C>800,C<900),  ""YES"", ""NO"")"