Excel VBA性能编码设置

我一直在研究如何在Excel VBA中加速我的代码,并且遇到以下有用的设置。 我的问题是:是否有可能将下列代码行设置为一个variables,我可以设置为打开或closures来激活整个列表? 也就是说

speedUpCode = On 

会设置下面的所有设置,如果设置为Off,则会将以下全部设置为True / xlCalculationAutomatic

 With Application .ScreenUpdating = False .DisplayStatusBar = False .Calculation = xlCalculationManual .EnableEvents = False End With ActiveSheet.DisplayPageBreaks = False 'note this is a sheet-level setting 

我使用这个(非常基本的):

 Sub GoFast(Optional bYesNo As Boolean = True) With Application .ScreenUpdating = Not bYesNo .Calculation = IIf(bYesNo, xlCalculationManual, xlCalculationAutomatic) End With End Sub 

True或者无参数调用来加速,然后用False来重置。

上面的评论关于可能捕获各种设置的当前状态,以便您可以回到“原始”状态,而且并不是所有的设置总是适合更新,这取决于您所做的事情是值得考虑的。

你可以使用一个函数来做到这一点,所以…

 Function speedUpCode(sStatus As String) If sStatus = "On" Then With Application .ScreenUpdating = False .DisplayStatusBar = False .Calculation = xlCalculationManual .EnableEvents = False End With ActiveSheet.DisplayPageBreaks = False 'note this is a sheet-level setting Else if sStatus = "Off" then With Application .ScreenUpdating = True .DisplayStatusBar = True .Calculation = xlCalculationAutomatic .EnableEvents = True End With ActiveSheet.DisplayPageBreaks = True 'note this is a sheet-level setting End Function 

您可以使用这些打开和closures

  speedUpCode "On" speedUpCode "Off" 

但是,请记住,您正在打开和closures设置 – 您应该检查它们的状态,然后再更改它们,以便您可以将它们重置为原始设置,而不是再次closures它们,您可以使用静态variables