优化vba应用程序。
这不是最重要的问题,更为安全。 我在所有子程序中都使用application.screenupdate等。 我通常会有多个模块和子程序,它们都有优化代码 我想知道是否有办法不必在每个模块中编写代码 我想我正在寻找一个类似于全局声明变量的概念,它可以在任何模块或子例程中使用,而无需每次都定义它们优化vba应用程序。,vba,excel,optimization,Vba,Excel,Optimization,这不是最重要的问题,更为安全。 我在所有子程序中都使用application.screenupdate等。 我通常会有多个模块和子程序,它们都有优化代码 我想知道是否有办法不必在每个模块中编写代码 我想我正在寻找一个类似于全局声明变量的概念,它可以在任何模块或子例程中使用,而无需每次都定义它们 with application .ScreenUpdating = False .DisplayStatusBar = False end with 你可以做一个代码外壳生成器。添加对
with application
.ScreenUpdating = False
.DisplayStatusBar = False
end with
你可以做一个代码外壳生成器。添加对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
你可以做一个代码外壳生成器。添加对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
如果函数设置为true,则可以声明布尔函数。ScreenUpdate设置为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
您可以在单个模块中创建此函数,然后在任何其他模块或表单中引用它。这将在每个模块中保存重复代码。如果这不是您想要的,请道歉。如果函数设置为true,则可以声明布尔函数。ScreenUpdate设置为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
您可以在单个模块中创建此函数,然后在任何其他模块或表单中引用它。这将在每个模块中保存重复代码。如果这不是您想要的,则表示歉意。更改代码中的应用程序设置的一个常见问题是,它们会覆盖用户首选项-例如,最终用户通常会将其计算设置为手动以处理大型工作簿,但每次运行您的代码时,最终都会将其设置为自动 为了克服这一问题,下面的子例程将当前应用程序设置分配给静态变量,并使用这些设置重置应用程序
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
致以最良好的祝愿,
Doug C在代码中更改应用程序设置的一个常见问题是,它们会覆盖用户首选项-例如,最终用户通常会将其计算设置为手动,以便处理大型工作簿,但每次运行代码时,最终都会将其设置为自动 为了克服这一问题,下面的子例程将当前应用程序设置分配给静态变量,并使用这些设置重置应用程序
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
致以最良好的祝愿,
Doug C您需要xlsb文件,如果您想共享excel代码或vbscript,也可以帮助您。对不起?我不明白你的意思…这使用类来持久化和恢复应用程序设置。您需要xlsb文件,如果您想共享excel代码或vbscript,也可以帮助您。对不起?我不明白你的意思…这使用类来持久化和恢复应用程序设置。或者其他类似的款式。这太好了!但不幸的是,这并不是我想要达到的目的。你的问题是如何避免锅炉铭牌代码。我的建议并没有避开锅炉板,而是通过自动生成它来节省击键次数。我同意一开始最好避免使用它。这很好!但不幸的是,这并不是我想要达到的目的。你的问题是如何避免锅炉铭牌代码。我的建议并没有避开锅炉板,而是通过自动生成它来节省击键次数。我同意首先最好避免使用它。“not”部分是关于什么的?
not
集合传递与ApplicationOptimization()相反的值。因此,如果将ApplicationOptimization()
设置为true,则.properties
将设置为false。您需要在ApplicationOptimization()
上添加if
或选择CASE
,以捕获布尔值。当ApplicationOptimization()=TRUE时,那么.calculation=xlCalculationManual
赋值左侧函数调用的错误必须返回Objectcgtapologies的值,我当时很忙。我已将代码更新为您应该能够使用的内容。这次我已经测试过了,效果很好。如果您还需要什么,请告诉我。“不”部分是关于什么的?not
集合传递的是ApplicationOptimization()
的相反值。因此,如果将ApplicationOptimization()
设置为true,则.properties
将设置为false。您需要在ApplicationOptimization()
上添加if
或选择CASE
,以捕获布尔值。当ApplicationOptimization()=TRUE时,那么.calculation=xlCalculationManual
赋值左侧函数调用的错误必须返回Objectcgtapologies的值,我当时很忙。我已将代码更新为您应该能够使用的内容。这次我已经测试过了,效果很好。如果你还需要什么,请告诉我。