我的VBA代码每次迭代都变得越来越慢

我正在使用代码来添加一个列,挖掘左侧的值(通过vlookup),然后自动填充整个列。 我遇到的问题是每次使用macros时,代码都会稍微长一些。 将不胜感激任何帮助:)

这里是代码:

Sub insert_col() ' ' insert_col Macro ' ' Keyboard Shortcut: Ctrl+w ' Dim x As Variant Dim a As Long Dim b As Long Dim y As Variant Dim t As Single Application.ScreenUpdating = False Application.Calculation = xlCalculationManual t = Timer ActiveSheet.Columns(ActiveCell.Column).EntireColumn.Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove x = ActiveCell.Column Cells(22, x).Select ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],R13C1:R193C2,1)" Cells(22, x).Select a = ActiveCell.Column x = ActiveCell.Row y = ActiveCell.End(xlDown).Row Selection.AutoFill Destination:=Range(Cells(x, a), Cells(36600, a)), Type:=xlFillDefault ActiveCell.Offset(0, 2).Select MsgBox Timer - t Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub 

谢谢!

为了更清楚地说明上面的注释,您的代码每次运行时都会添加更多的公式,这会增加计算时间。

你可以更简单的代码沿着下面的代码:

 Sub insert_col() ' Keyboard Shortcut: Ctrl+w Application.ScreenUpdating = False Application.Calculation = xlCalculationManual t = Timer ActiveCell.EntireColumn.Insert Range(Cells(22, ActiveCell.Column), Cells(36600, ActiveCell.Column)).FormulaR1C1 = "=VLOOKUP(RC[-1],R13C1:R193C2,1)" ActiveCell.Offset(0, 2).Select MsgBox Timer - t Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub