公式/ VBA-应用程序定义或对象定义的错误

时间:2018-10-29 20:26:45

标签: excel vba excel-formula

我正在尝试创建一个VBA代码,以便在工作表上的确定单元格中编写公式,但是它不起作用。怎么了 ?因为,我真的看不出这段代码在做错什么。

Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"

1 个答案:

答案 0 :(得分:2)

您的计算机将;用作区域列表分隔符使您很头疼。 VBA非常以EN-US为中心,因此Range.FormulaRange.FormulaR1C1期望逗号(,)作为函数的参数列表分隔符。

Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, " & """" & "DROP" & """" & ", " & """" & "REVIEW" & """" & "), " & """" & "REVIEW" & """" & "), " & """" & """" & ")"

或者,Range.FormulaLocal propertyRange.FormulaR1C1Local property可以与您自己的分号一起用作列表分隔符。

Range("BC" & ActiveCell.Row).FormulaLocal = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"

您将带引号的字符串中的双引号加倍有点冗长,xlR1C1表示法将节省一些步骤。 TEXT(,)与公式中的""相同;每个都产生相同的零长度字符串。

Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=""REVIEW"", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=""OK"", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
'.FormulaR1C1
Range("BC" & ActiveCell.Row).FormulaR1C1 = "=IF(RC54=""REVIEW"", IF(ROW(R:R)<MAX(IF(C54:C54=""OK"", C1:C1)), IF(TODAY()-RC51>=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"

ActiveCell在第二行时,上面重写的xlA1和xlR1C1公式会产生以下结果。

=IF($BB2="REVIEW", IF(ROW($BB2)<MAX(IF($BB:$BB="OK", $A:$A)), IF(TODAY()-$AY2>=3, "DROP", "REVIEW"), "REVIEW"), TEXT(,))

正如我在上面的评论中提到的那样,该公式似乎是一个数组公式。在这种情况下,请使用基于.Formula逗号的重写,但请将Range.Formula属性更改为Range.FormulaArray