简体   繁体   English

VBA运行时错误1004-object_Global的范围失败-IF公式

[英]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调用进行限定,并且由于要分配公式 ,因此您应该明确地说Rangedefault属性指向其value ,因此您利用了很多隐式行为为使该代码起作用:最好使尽可能明确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA运行时错误1004 Object_Global的范围失败 - VBA Run Time Error 1004 Range of Object_Global Failed VBA运行时错误1004的object_global范围失败 - VBA Runtime error 1004 range of object_global failed 在带有命名范围的Excel中搜索时,object_global的运行时错误1004方法范围失败 - Run time error 1004 method range of object_global failed on a search in Excel with a named range 运行时错误'1004'-VBA中对象'_Global'的方法'Range'失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed in VBA Excel VBA 运行时错误'1004'对象'_Global'的方法'范围'失败 - Excel VBA Run-Time Error '1004' Method 'Range' of object'_Global' Failed VBA-运行时错误'1004'-对象'_Global'的方法'Range'失败 - VBA - Run-time error '1004' - Method 'Range' of object'_Global' failed VBA运行时错误'1004':对象'_Global'的方法'Range'失败 - VBA Run-time error '1004': Method 'Range' of object '_Global' Failed 损坏的Excel VBA宏运行时错误'1004':对象'_Global'的方法'Range'失败 - Broken Excel VBA Macro Run-time error '1004': Method 'Range' of object '_Global' failed _Global的VBA错误1004对象范围失败 - VBA Error 1004 Object Range failed for _Global 运行时错误 '1004' 'Range' of object'_Global' 失败 - Run-time error '1004' 'Range' of object'_Global' failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM