运行时错误' 1004':方法' FormulaR1C1'对象'范围'失败

时间:2016-01-11 23:22:29

标签: excel vba excel-vba methods range

我正在尝试创建一个简单的宏,它基本上是添加一个新的列,命名它,粘贴公式并填充。我得到的错误与主题一样:

  

运行时错误' 1004':方法' FormulaR1C1'对象'范围'失败

这是公式:

=IF(OR($G2=DATA!$L$3;G2=DATA!$L$4;$G2=DATA!$L$5;$G2=DATA!$L$6;$G2=DATA!$L$7;$G2=DATA!$L$8;$G2=DATA!$L$9;$G2=DATA!$L$10);DATA!$L$2; IF(OR($G2=DATA!$M$3;G2=DATA!$M$4;$G2=DATA!$M$5;$G2=DATA!$M$6;$G2=DATA!$M$7;$G2=DATA!$M$8;$G2=DATA!$M$9;$G2=DATA!$M$10);DATA!$M$2; IF(OR($G2=DATA!$N$3;G2=DATA!$N$4;$G2=DATA!$N$5;$G2=DATA!$N$6;$G2=DATA!$N$7;$G2=DATA!$N$8;$G2=DATA!$N$9;$G2=DATA!$N$10);DATA!$N$2; IF(OR($G2=DATA!$O$3;G2=DATA!$O$4;$G2=DATA!$O$5;$G2=DATA!$O$6;$G2=DATA!$O$7;$G2=DATA!$O$8;$G2=DATA!$O$9;$G2=DATA!$O$10);DATA!$O$2; IF(OR($G2=DATA!$P$3;G2=DATA!$P$4;$G2=DATA!$P$5;$G2=DATA!$P$6;$G2=DATA!$P$7;$G2=DATA!$P$8;$G2=DATA!$P$9;$G2=DATA!$P$10);DATA!$P$2; "OTHER")))))

宏观:

Sub Macro16()
'
' Macro16 Macro
'

'
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "CSS Team"
    Range("I2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(RC7=DATA!R3C12,RC[-2]=DATA!R4C12,RC7=DATA!R5C12,RC7=DATA!R6C12,RC7=DATA!R7C12,RC7=DATA!R8C12,RC7=DATA!R9C12,RC7=DATA!R10C12),DATA!R2C12, IF(OR(RC7=DATA!R3C13,RC[-2]=DATA!R4C13,RC7=DATA!R5C13,RC7=DATA!R6C13,RC7=DATA!R7C13,RC7=DATA!R8C13,RC7=DATA!R9C13,RC7=DATA!R10C13),DATA!R2C13, IF(OR(RC7=DATA!R3C14,RC[-2]=DATA!R4C14,RC7=DATA!R5C14,RC7=DATA!R6C14,RC7=DATA!R7C" & _
        "ATA!R8C14,RC7=DATA!R9C14,RC7=DATA!R10C14),DATA!R2C14, IF(OR(RC7=DATA!R3C15,RC[-2]=DATA!R4C15,RC7=DATA!R5C15,RC7=DATA!R6C15,RC7=DATA!R7C15,RC7=DATA!R8C15,RC7=DATA!R9C15,RC7=DATA!R10C15),DATA!R2C15, IF(OR(RC7=DATA!R3C16,RC[-2]=DATA!R4C16,RC7=DATA!R5C16,RC7=DATA!R6C16,RC7=DATA!R7C16,RC7=DATA!R8C16,RC7=DATA!R9C16,RC7=DATA!R10C16),DATA!R2C16, ""OTHER"")))))"
    Range("I2").Select
    Selection.AutoFill Destination:=Range("I2:I21445")
    Range("I2:I21445").Select
End Sub

1 个答案:

答案 0 :(得分:2)

对于663个字符,您的公式可能太长而无法填入Range.Formula propertyRange.FormulaR1C1 property的单元格中,但主要关注的是VBA非常以EN-US为中心且您的公式必须使用逗号作为列表分隔符传递到属性,而不是半角,无论计算机的区域系统将分号指定为列表分隔符,并且在工作表中键入公式时使用分号

>>> faces = {1: 'Ace', 11: 'Jack', 12: 'Queen', 13: 'King'}
>>> faces[1]
'Ace'
>>> faces[13]
'King'
>>> cards = [1,1,1,1,2,2,2,2,3,3,3,3,11]
>>> cards[1]
1
>>> faces[cards[1]]
'Ace'

如果您绝对需要使用分号,可以将重新分隔的公式与Range.FormulaLocal property一起使用。

<强> -Addendum:

我已将 xlA1 风格的公式减少到98个字符。

=IF(OR($G2=DATA!$L$3,G2=DATA!$L$4,$G2=DATA!$L$5,$G2=DATA!$L$6,$G2=DATA!$L$7,$G2=DATA!$L$8,$G2=DATA!$L$9,$G2=DATA!$L$10), DATA!$L$2, 
 IF(OR($G2=DATA!$M$3,G2=DATA!$M$4,$G2=DATA!$M$5,$G2=DATA!$M$6,$G2=DATA!$M$7,$G2=DATA!$M$8,$G2=DATA!$M$9,$G2=DATA!$M$10), DATA!$M$2, 
 IF(OR($G2=DATA!$N$3,G2=DATA!$N$4,$G2=DATA!$N$5,$G2=DATA!$N$6,$G2=DATA!$N$7,$G2=DATA!$N$8,$G2=DATA!$N$9,$G2=DATA!$N$10), DATA!$N$2, 
 IF(OR($G2=DATA!$O$3,G2=DATA!$O$4,$G2=DATA!$O$5,$G2=DATA!$O$6,$G2=DATA!$O$7,$G2=DATA!$O$8,$G2=DATA!$O$9,$G2=DATA!$O$10), DATA!$O$2, 
 IF(OR($G2=DATA!$P$3,G2=DATA!$P$4,$G2=DATA!$P$5,$G2=DATA!$P$6,$G2=DATA!$P$7,$G2=DATA!$P$8,$G2=DATA!$P$9,$G2=DATA!$P$10), DATA!$P$2,
   "OTHER")))))

您的原始公式在F2填充到F8。上面提出的公式在E2中填写为E8。

semi_colon_formula