优化vba - 应用程序。

时间:2016-11-07 11:15:32

标签: vba excel-vba optimization excel

这不是最重要的问题,而是更加可怜。 我在所有子程序中使用application.screenupdating等。 我通常会有多个模块和子程序,所有这些都有优化代码。

我想知道是否有办法不必在每个模块中编写代码?

我想我正在寻找一个类似于全局声明的变量的概念,可以在任何模块或子程序中使用,而不必每次都定义它们

with application
    .ScreenUpdating = False
    .DisplayStatusBar = False
end with

3 个答案:

答案 0 :(得分:2)

你可以声明一个布尔函数,如果函数设置为true,那么.ScreenUpdating设置为false,反之亦然。

Public Function ApplicationOptimization(BOOLEAN_TRIGGER As Boolean)
With Application
    Select Case BOOLEAN_TRIGGER
    Case True
    .ScreenUpdating = False
    .DisplayStatusBar = False
    .Calculation = xlCalculationManual
    Case False
    .ScreenUpdating = True
    .DisplayStatusBar = True
    .Calculation = xlCalculationAutomatic
    End Select
End With
End Function

Public Sub Use()
ApplicationOptimization (False) 'False or True
End Sub

您可以在单个模块中创建此功能,然后以任何其他模块或形式引用它。这应该在每个模块中保存重复代码。如果这不是你想要的,那就道歉。

答案 1 :(得分:0)

你可以创建一个代码shell生成器。添加对Microsoft Forms 2.0对象库的引用,然后:

Public Sub SubShell(SubName As String)
    Dim s As String
    Dim DataObj As New MSForms.DataObject

    s = "Sub " & SubName & "()" & vbCrLf
    s = s & String(4, " ") & "With Application" & vbCrLf
    s = s & String(8, " ") & ".ScreenUpdating = False" & vbCrLf
    s = s & String(8, " ") & ".DisplayStatusBar = False" & vbCrLf
    s = s & String(4, " ") & "End With" & vbCrLf & vbCrLf
    s = s & String(4, " ") & "'Insert Code" & vbCrLf & vbCrLf
    s = s & String(4, " ") & "With Application" & vbCrLf
    s = s & String(8, " ") & ".ScreenUpdating = True" & vbCrLf
    s = s & String(8, " ") & ".DisplayStatusBar = True" & vbCrLf
    s = s & String(4, " ") & "End With" & vbCrLf
    s = s & "End Sub" & vbCrLf
    DataObj.SetText s
    DataObj.PutInClipboard
End Sub

如果您现在在即时窗口中键入SubShell "Test",请将光标放在您想要子的位置,然后粘贴,您将看到以下内容:

Sub Test()
    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
    End With

    'Insert Code

    With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
    End With
End Sub

答案 2 :(得分:0)

更改代码中的应用程序设置的常见问题是它们会覆盖用户首选项-例如最终用户通常将他们的“计算”设置为手动,以处理大型工作簿,但是每次他们运行代码时,最终都会重新设置为“自动”。

为克服这个问题,下面的子例程将当前应用程序设置分配给静态变量,并使用这些变量重置应用程序。

Private Sub optimise_execution(optimise As Boolean)
' Optimise / Reset application settings for running VBA code
Static calcStr As Variant
Static screenBool As Boolean, statusBool As Boolean, eventsBool As Boolean, displayBreaksBool As Boolean
        If Not optimise Then GoTo RestoreSettings
' Clean up variables
            calcStr = vbNullString: screenBool = False: statusBool = False: eventsBool = False: displayBreaksBool = False
StoreSettings:          ' Store the current application settings
            calcStr = Application.Calculation
            screenBool = Application.ScreenUpdating
            eventsBool = Application.EnableEvents
            displayBreaksBool = ActiveSheet.DisplayPageBreaks
OptimiseSettings:       ' Set optimum settings
            Application.Calculation = xlCalculationManual
            Application.ScreenUpdating = False
            Application.DisplayStatusBar = False
            Application.EnableEvents = False
            ActiveSheet.DisplayPageBreaks = False
            Application.DisplayAlerts = False
            Exit Sub
RestoreSettings:        ' Restore previous application settings
            Application.Calculation = calcStr
            Application.ScreenUpdating = screenBool
            Application.DisplayStatusBar = True
            Application.EnableEvents = eventsBool
            ActiveSheet.DisplayPageBreaks = displayBreaksBool
            Application.DisplayAlerts = True
End Sub

Public Sub Main()
    Call optimise_execution(True)   ' True to optimise / False to reset
End Sub

最好的问候, 道格C