2012-04-19 120 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只是運行宏,沒有屏幕更新或計算,直到宏完成。