VBA自动化FormulaR1C1

时间:2016-11-01 09:35:15

标签: vba

我需要将每个单元格从Row 3 Col 3开始,以Row 3 Col 32结尾不同的公式,29次。

如何自动执行此查询,因为29是变量。

Range("C3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[6],Importat!C[-2],R2C3,Importat!C[5],RC[-2],Importat!C[-1],RC[-1])"

Range("D3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[15],Importat!C[7],R2C4,Importat!C[14],RC[-3],Importat!C[8],RC[-2])"

Range("E3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[24],Importat!C[16],R2C5,Importat!C[23],RC[-4],Importat!C[17],RC[-3])"

Range("F3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[33],Importat!C[25],R2C6,Importat!C[32],RC[-5],Importat!C[26],RC[-4])"

Range("G3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[42],Importat!C[34],R2C7,Importat!C[41],RC[-6],Importat!C[35],RC[-5])"

Range("H3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[51],Importat!C[43],R2C8,Importat!C[50],RC[-7],Importat!C[44],RC[-6])"

...........

Range("AE3").Select
ActiveCell.FormulaR1C1 = _
    "=SUMIFS(Importat!C[258],Importat!C[250],R2C32,Importat!C[257],RC[-30],Importat!C[251],RC[-29])"

1 个答案:

答案 0 :(得分:2)

试试这个:

Dim i As Long
With Range("C3")
    For i = 1 To 29
        .Offset(, i - 1).FormulaR1C1 = "=SUMIFS(Importat!C[" & 6 + (i - 1) * 9 & "],Importat!C[" & -2 + (i - 1) * 9 & "],R2C,Importat!C[" & 5 + (i - 1) * 9 & "],RC1,Importat!C[" & -1 + (i - 1) * 9 & "],RC2)"
    Next i
End With