如何提高此宏的速度?
我的宏是:
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
答案 0 :(得分:2)
在此处查看其他宏速度改进:
特别是对于您的代码,您可以更改:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
(这是可选的,但我通常会把它放在我的代码中以防我的代码中有事件)如果你不想在你的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