2014-03-13 109 views
2

我該如何提高這個宏的速度?我可以使這個宏更快嗎?

我的宏:

Attribute VB_Name = "RemplazoString" 
Sub MACRO() 
    bAlerts = Application.DisplayAlerts 
    Application.DisplayAlerts = False 
    For i = 1 To Worksheets.Count 
     Worksheets(i).Cells.Replace What:="T:\", Replacement:="T:\Gestion\", LookAt:=xlPart 
    Next 
    Application.DisplayAlerts = bAlerts 
End Sub 

回答

2

在這裏尋找更多的宏觀速度的改進:

特別是對於你的代碼,你可以改變:

  1. Application.ScreenUpdating = False
  2. Application.Calculation = xlCalculationManual
  3. Application.EnableEvents = False(這是可選的,但我通常把它放在我的代碼的情況下,我在我的代碼的事件)

此外,我會小心上面的如果你不想凍結你的Excel窗口,如果你的Sub內部發生了錯誤,請加上一個en錯誤處理程序。

所以,你的代碼看起來像如下:

Attribute VB_Name = "RemplazoString" 
Sub MACRO() 
    On Error GoTo ErrHandler: 
    'Speed Up block 
    Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 
    Application.EnableEvents = False 
    '--- 
    bAlerts = Application.DisplayAlerts 
    Application.DisplayAlerts = False 
    For i = 1 To Worksheets.Count 
     Worksheets(i).Cells.Replace What:="T:\", Replacement:="T:\Gestion\", LookAt:=xlPart 
    Next 
    Application.DisplayAlerts = bAlerts 

    'Leave speed up settings as default 
    Application.ScreenUpdating = True 
    Application.Calculation = xlCalculationAutomatic 
    Application.EnableEvents = True 
    '--- 
Exit Sub 

ErrHandler: 
    'Leave speed up settings as default 
    Application.ScreenUpdating = True 
    Application.Calculation = xlCalculationAutomatic 
    Application.EnableEvents = True 
End Sub 
+0

+1也會推薦一個「每個」優越於「爲我」的做法。 – brettdj