无法设置Range类的FormulaArray属性 - 如何缩短变量

时间:2016-06-09 13:38:02

标签: excel excel-vba vba

我已经有了这个脚本(我创建了一个基本宏,然后继续编辑它以完成我需要的所有工作),除了最后2个公式之外,它们的工作方式非常完美,这对于FormulaArray来说太大了。我相信限制是255,这些公式大约是260个空格。

Sub Expenses2()
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveCell.FormulaR1C1 = "ID"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Names"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Weekend"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Team Leader"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Expense 1"
Range("F1").Select
ActiveCell.FormulaR1C1 = "1 £"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Expense 2"
Range("H1").Select
ActiveCell.FormulaR1C1 = "2 £"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Expense 3"
Range("J1").Select
ActiveCell.FormulaR1C1 = "3 £"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Expense 4"
Range("L1").Select
ActiveCell.FormulaR1C1 = "4 £"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Expense 5"
Range("N1").Select
ActiveCell.FormulaR1C1 = "5 £"
Range("A2").Select
ActiveCell.FormulaR1C1 = "='PSC Audit Report 3.0'!RC"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
    "=INDEX('p:\_new P Drive_\accounts department\myname\[Customer Date - ID & Name.xls]Customer Data'!R2C3:R30000C3,MATCH(RC[-1],'P:\_NEW P DRIVE_\ACCOUNTS DEPARTMENT\myname\[Customer Data - ID & Name.xls]Customer Data'!R2C2:R30000C2,0))"
    Range("C2").Select
ActiveCell.FormulaR1C1 = "='PSC Audit Report 3.0'!RC[-1]"
Range("D2").Select
ActiveCell.FormulaR1C1 = "='PSC Audit Report 3.0'!RC[-1]"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
    "=INDEX('PSC Audit Report 3.0'!R1C4:R1C46,MATCH(TRUE,INDEX('PSC Audit Report 3.0'!RC[-1]:RC[41]>0,),0))"
Range("F2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!D2:AT2,MATCH(TRUE,INDEX('PSC Audit Report 3.0'!D2:AT2>0,),0))"
Range("G2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!$D$1:$AT$1,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2),0))"
Range("H2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!D2:AT2,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2),0))"
Range("I2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!$D$1:$AT$1,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2)*('PSC Audit Report 3.0'!D2:AT2<>H2),0))"
Range("J2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!D2:AT2,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2)*('PSC Audit Report 3.0'!D2:AT2<>H2),0))"
Range("K2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!$D$1:$AT$1,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2)*('PSC Audit Report 3.0'!D2:AT2<>H2)*('PSC Audit Report 3.0'!D2:AT2<>J2),0))"
Range("L2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!D2:AT2,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2)*('PSC Audit Report 3.0'!D2:AT2<>H2)*('PSC Audit Report 3.0'!D2:AT2<>J2),0))"
Range("M2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!$D$1:$AT$1,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2)*('PSC Audit Report 3.0'!D2:AT2<>H2)*('PSC Audit Report 3.0'!D2:AT2<>J2)*('PSC Audit Report 3.0'!D2:AT2<>L2),0))"
Range("N2").Select
ActiveCell.FormulaArray = "=INDEX('PSC Audit Report 3.0'!D2:AT2,MATCH(1,INDEX('PSC Audit Report 3.0'!D2:AT2>0,)*('PSC Audit Report 3.0'!D2:AT2<>F2)*('PSC Audit Report 3.0'!D2:AT2<>H2)*('PSC Audit Report 3.0'!D2:AT2<>J2)*('PSC Audit Report 3.0'!D2:AT2<>L2),0))"

End Sub

我已将此更改为:

Sub Expenses()
'
' Expenses Macro
 '
Dim sAudit As String
sAudit = "PSC Audit Report 3.0"
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveCell.FormulaR1C1 = "ID"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Names"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Weekend"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Team Leader"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Expense 1"
Range("F1").Select
ActiveCell.FormulaR1C1 = "1 £"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Expense 2"
Range("H1").Select
ActiveCell.FormulaR1C1 = "2 £"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Expense 3"
Range("J1").Select
ActiveCell.FormulaR1C1 = "3 £"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Expense 4"
Range("L1").Select
ActiveCell.FormulaR1C1 = "4 £"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Expense 5"
Range("N1").Select
ActiveCell.FormulaR1C1 = "5 £"
Range("A2").Select
ActiveCell.FormulaR1C1 = "='sAudit'!RC"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
    "=INDEX('p:\_new P Drive_\accounts department\steven mack\[Customer Date - ID & Name.xls]Customer Data'!R2C3:R30000C3,MATCH(RC[-1],'P:\_NEW P DRIVE_\ACCOUNTS DEPARTMENT\Steven Mack\[Customer Data - ID & Name.xls]Customer Data'!R2C2:R30000C2,0))"
Range("C2").Select
ActiveCell.FormulaR1C1 = "='sAudit'!RC[-1]"
Range("D2").Select
ActiveCell.FormulaR1C1 = "='sAudit'!RC[-1]"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
    "=INDEX('sAudit'!R1C4:R1C46,MATCH(TRUE,INDEX('sAudit'!RC[-1]:RC[41]>0,),0))"
Range("F2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!D2:AT2,MATCH(TRUE,INDEX('sAudit'!D2:AT2>0,),0))"
Range("G2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!$D$1:$AT$1,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2),0))"
Range("H2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!D2:AT2,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2),0))"
Range("I2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!$D$1:$AT$1,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2)*('sAudit'!D2:AT2<>H2),0))"
Range("J2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!D2:AT2,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2)*('sAudit'!D2:AT2<>H2),0))"
Range("K2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!$D$1:$AT$1,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2)*('sAudit'!D2:AT2<>H2)*('sAudit'!D2:AT2<>J2),0))"
Range("L2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!D2:AT2,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2)*('sAudit'!D2:AT2<>H2)*('sAudit'!D2:AT2<>J2),0))"
Range("M2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!$D$1:$AT$1,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2)*('sAudit'!D2:AT2<>H2)*('sAudit'!D2:AT2<>J2)*('sAudit'!D2:AT2<>L2),0))"
Range("N2").Select
ActiveCell.FormulaArray = "=INDEX('sAudit'!D2:AT2,MATCH(1,INDEX('sAudit'!D2:AT2>0,)*('sAudit'!D2:AT2<>F2)*('sAudit'!D2:AT2<>H2)*('sAudit'!D2:AT2<>J2)*('sAudit'!D2:AT2<>L2),0))"

End Sub

然而,我的所有公式都以sAudit结尾,而不是PSC Audit Report 3.0。 :/

此外,不是这些问题,但对于单元格A2到N2,它会一直打开一个窗口来选择另一个文件。我每次都可以点击取消,但这很令人讨厌。有没有人遇到过这个?

1 个答案:

答案 0 :(得分:0)

你可以简单地使用最后两个非数组公式:

NewObject