如何加快VBA以快速填充这些公式?

时间:2016-05-25 18:45:30

标签: excel vba excel-vba sumifs

Sheets("RMI Static Compare").Range("G2:G" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])"
Sheets("RMI Static Compare").Range("H2:H" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "=SUMIF('RMI Previous'!C[-7],'RMI Static Compare'!C[-7],'RMI Previous'!C[31])"
Sheets("RMI Static Compare").Range("I2:I" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "ABS(RC[-2])"
Sheets("RMI Static Compare").Range("J2:J" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "RC[-3]-RC[-2]"
Sheets("RMI Static Compare").Range("K2:K" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "=IFERROR(((RC[-4]-RC[-3])/RC[-3]),""Zero Balance"")"
Sheets("RMI Static Compare").Range("L2:L" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "=IF(RC[-3]>500000,""Material"",""Immaterial"")"
Sheets("RMI Static Compare").Range("M2:M" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "=IF(AND(RC[-2]>-0.01,RC[-2]<0.01),""Y"",""N"")"

我有超过80,000行来容纳这些公式,我需要公式停在A列的最后一个单元格/快速填充答案而不需要每个10分钟。请帮忙!

1 个答案:

答案 0 :(得分:1)

在不知道其余代码的情况下,我建议添加lastRow变量和工作表变量以节省一些编码空间:

Sub t()
Dim lastRow&
Dim rmiWS As Worksheet
Set rmiWS = Sheets("RMI Static Compare")

lastRow = rmiWS.Cells(rmiWS.Rows.Count, "A").End(xlUp).Row

With rmiWS
    .Range("G2:G" & lastRow).FormulaR1C1 = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])"
End With

'The above `With` statement is the exact same as the below line. Notice you can use `.` as a placeholder for whatever follows `With`
' Sheets("RMI Static Compare").Range("G2:G" & Sheets("RMI Static Compare").Cells(Sheets("RMI Static Compare").Rows.Count, "A").End(xlUp).Row).Formula = .Range("G2:G" & lastRow).FormulaR1C1 = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])"

End Sub

注意. Range()之前的With,它有效地附加了&#34;对该陈述R1C1之后的任何内容。这应该对你有帮助。

此外,根据您的公式,您希望使用lastRow格式。

现在的问题是,Sheets("RMI Static Compare").Range("G2:G" & Cells(Rows.Count, "F").End(xlUp).Row).Formula = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])" Sheets做了你想做的事吗?

编辑:哇,自从这个答案以来,原来的问题已经改变了......但理论仍然适用于你的所有公式。

基本上,这个: Sheets("RMI Static Compare").Range("G2:G" & Sheets("RMI Static Compare").Cells(Sheets("RMI Static Compare").Rows.Count, "F").End(xlUp).Row).FormulaR1C1 = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])"

需要是:

With Sheets("RMI Static Compare")
    .Range("G2:G" & .Cells(.Rows.Count, "F").End(xlUp).Row).FormulaR1C1 = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])"
End with

With Sheets("RMI Static Compare")
   .Range("G2:G2").Resize(.Cells(.Rows.Count, "F").End(xlUp).row - 1).FormulaR1C1 = "=SUMIF('RMI Current'!C[-6],'RMI Static Compare'!C[-6],'RMI Current'!C[32])"
End With

编辑2:要将公式放在某个范围内,并自动更新,这样您就不必拖动,请执行以下操作:

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

您只需要为单元格设置范围,然后调整行数 - 1

Editx:另外,将这些行添加到Sub的顶部,将其他行添加到底部:

 Application.ScreenUpdating = True
 Application.Calculation = xlCalculationAutomatic

然后在底部

{{1}}