VBA – 将包含引号的Excel公式设置为单元格值
我想让我的VB脚本input一个Excel公式到一个单元格。 不幸的是我到目前为止没有运气。
Sheets("ABC").Select Range("B2").Value = "=IF(Mat_Location!F2=0;"";Mat_Location!F2)"
一些谷歌search和search这个网站后,我觉得这是因为引号,所以我试图改变引号双引号。
Sheets("ABC").Select Range("B2").Value = "=IF(Mat_Location!F2=0;"""";Mat_Location!F2)"
这也没有帮助。
任何帮助表示赞赏。 提前致谢。
replace你的多个"
与Chr(34)
,这样更容易debugging。
另外,没有必要先Select
表格。
Sheets("ABC").Range("B2").FormulaLocal = "=IF(Mat_Location!F2=0;" & Chr(34) & Chr(34) & ";Mat_Location!F2)"
注意 :看来你的Excel的区域设置是有的;
作为公式中的分隔符。 默认设置是,
所以对于我的Excel设置(也可能是你的),它可能是:
Sheets("ABC").Range("B2").Formula = "=IF(Mat_Location!F2=0," & Chr(34) & Chr(34) & ",Mat_Location!F2)"