2012-04-19 119 views
2

下面的代码有什么问题?我正在尝试vlookup与另一个工作簿中的已关闭数据。如果我运行此代码,则需要很长时间才能执行。最后它会挂起......但它最终会产生正确的结果:Vba -excel .Formula方法很慢

Sub testing() 
    Range("G1").EntireColumn.Insert 
    With Range("G2") 
    .Formula = "=VLOOKUP(F2,'C:\Users\sathisk\Desktop\Macros\ReferencePath\[HUL_Assets_18th April 2012.xls]HUL_Assets_18th April 2012.xls'!A$1:J$65536,10,0)" 
    .Copy 
    .Offset(0, -1).Select 
    End With 

    Selection.End(xlDown).Offset(0, 1).Select 

    With ActiveCell 
    .PasteSpecial xlPasteFormulas 
    .Copy 
    .Select 
    End With 

    Range(ActiveCell, Selection.End(xlUp)).PasteSpecial xlPasteFormulas 

End Sub 

回答

2

这是你正在尝试做什么?

Option Explicit 

Sub testing() 
    Dim LastRow As Long 

    Application.Calculation = xlCalculationManual 

    Range("G1").EntireColumn.Insert 

    LastRow = Range("F" & Rows.Count).End(xlUp).Row 

    With Range("G2:G" & LastRow) 
     .Formula = "=VLOOKUP(F2,'C:\Users\sathisk\Desktop\Macros\ReferencePath\[HUL_Assets_18th April 2012.xls]HUL_Assets_18th April 2012.xls'!A$1:J$65536,10,0)" 
    End With 

    Application.Calculation = xlCalculationAutomatic 
End Sub 

编辑

我也建议你改变J$65536在该表的实际最后一排。例如,如果在该表中的数据是直到1000行然后将其改为J$1000

+0

:)谢谢代码Siddharth ..原因为何使用范围J $ 65536 .. Vloopup从封闭的工作簿中选取数据..其中工作簿每天都在变化(增加和减少)d行.. – 2012-04-20 05:03:35

1

除了亚洲时报Siddharth建议我还要补充

Application.ScreenUpdating =假“关闭屏幕更新,以加快处理。 在宏观与 Application.Calculation沿顶= xlCalculationManual

然后添加在屏幕上更新 Application.ScreenUpdating = TRUE“转向。 在底部 Application.Calculation = xlCalculationAutomatic

我使用这两种方法释放CPU只是运行宏,没有屏幕更新或计算,直到宏完成。