无法从vba插入公式

时间:2011-11-14 11:30:43

标签: excel vba

我想使用vba插入这些公式,但它会抛出错误的应用程序定义错误

activesheet.range("A4").formula = "=IF(AND($B4>TODAY() -7,$B4 < TODAY()),"ONE WEEK OLD",IF(AND($B4 < TODAY() -7,$B4 > TODAY() - 30),"ONE WEEK TO ONE MONTH OLD",IF(AND($B4 < TODAY()-30,$B4 > TODAY()-90),"1 MONTH TO 90 DAYS OLD","OLDER THAN 90 DAYS")))"

我使用了串联,也使用了字符串变量,但我仍然无法做到。

如果公式在vba中有双引号,我无法在单元格中插入公式

请帮我解决这些问题

1 个答案:

答案 0 :(得分:3)

您需要转义字符串中的引号。要做到这一点,只需加倍:

activesheet.range("A4").formula = "=IF(AND($B4>TODAY() -7,$B4 < TODAY()),""ONE WEEK OLD"",IF(AND($B4 < TODAY() -7,$B4 > TODAY() - 30),""ONE WEEK TO ONE MONTH OLD"",IF(AND($B4 < TODAY()-30,$B4 > TODAY()-90),""1 MONTH TO 90 DAYS OLD"",""OLDER THAN 90 DAYS"")))"

如果这没有帮助,您需要发布更多代码。