我可以更快地制作这个宏吗?

时间:2014-03-13 09:09:55

标签: excel excel-vba vba

如何提高此宏的速度?

我的宏是:

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

1 个答案:

答案 0 :(得分:2)

在此处查看其他宏速度改进:

特别是对于您的代码,您可以更改:

  1. Application.ScreenUpdating = False
  2. Application.Calculation = xlCalculationManual
  3. Application.EnableEvents = False(这是可选的,但我通常会把它放在我的代码中以防我的代码中有事件)
  4. 如果你不想在你的Sub中发生错误时想要冻结你的Excel窗口,我会小心使用一个错误处理程序。

    所以你的代码如下所示:

    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