[英]VBA Run Time Error 1004 - Range of object_Global failed - IF Formula
I've inserted the below formula into VBA but am getting VBA Run Time Error 1004 - "Range of object_Global failed" - not sure what I'm missing. 我已将以下公式插入VBA,但出现VBA运行时错误1004-“ object_Global的范围失败”-不知道我缺少什么。
Formula works in Excel but not when I translate to VBA. 公式在Excel中有效,但当我转换为VBA时无效。
Range("AF") ="=IF(OR(AND(LEN('ACCOUNT DATA'!V2)=8,LEFT('ACCOUNT DATA'!V2,2)=""60""),LEFT('ACCOUNT DATA'!V2,3)=""CSN""),""CDR Created"",IF(OR(AND(LEN('ACCOUNT DATA'!X2)=8,LEFT('ACCOUNT DATA'!X2,2)=""60""),LEFT('ACCOUNT DATA'!X2,3)=""CSN""),""CDR Created"",IF(OR(AND(LEN('ACCOUNT DATA'!Y2)=8,LEFT('ACCOUNT DATA'!Y2,2)=""60""),LEFT('ACCOUNT DATA'!Y2,3)=""CSN""),""CDR Created"",""CDR Pending"")))"
CORRECTED FORMULA: I was missing the row number in my range... so AF2 instead of AF: 更正公式:我错过了我范围内的行号...所以AF2代替了AF:
Range("AF2") ="=IF(OR(AND(LEN('ACCOUNT DATA'!V2)=8,LEFT('ACCOUNT DATA'!V2,2)=""60""),LEFT('ACCOUNT DATA'!V2,3)=""CSN""),""CDR Created"",IF(OR(AND(LEN('ACCOUNT DATA'!X2)=8,LEFT('ACCOUNT DATA'!X2,2)=""60""),LEFT('ACCOUNT DATA'!X2,3)=""CSN""),""CDR Created"",IF(OR(AND(LEN('ACCOUNT DATA'!Y2)=8,LEFT('ACCOUNT DATA'!Y2,2)=""60""),LEFT('ACCOUNT DATA'!Y2,3)=""CSN""),""CDR Created"",""CDR Pending"")))"
That error usually means you're implicitly referring to the active sheet somewhere that you shouldn't be, for example: 该错误通常意味着您隐式地在不应该引用的地方引用了活动工作表,例如:
Sheet2.Activate
Sheet1.Range(Cells(1,1), Cells(2,2)) = 42 'boom: "Cells" is referring to Sheet2
Here you're getting it because Range("AF")
doesn't mean much to Excel. 在这里,您得到它是因为
Range("AF")
对Excel的意义不大。 Try this instead: 尝试以下方法:
[ActiveSheet.]Range("AF:AF").Formula = "..."
You'll probably want to calculate the actual rows though, because that will put the formula on every single row of column AF , which I doubt is what you really want. 不过,您可能想要计算实际的行,因为这会将公式放在列AF的每一行上 ,我怀疑这是您真正想要的。
Note, you should qualify that Range
call with a proper Worksheet
object, and since you're assigning the formula you should say so explicitly - a Range
's default property points to its value , so you're leveraging quite a lot of implicit behavior for that code to work: best make as much as possible explicit . 请注意,您应该使用适当的
Worksheet
对象对Range
调用进行限定,并且由于要分配公式 ,因此您应该明确地说Range
的default属性指向其value ,因此您利用了很多隐式行为为使该代码起作用:最好使尽可能明确 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.