Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
I have a sub that optimizes performance by shutting off certain functions (.ScreenUpdating, .DisplayAlerts, etc) at the beginning of a sub or function and turning them back on at the end. It works great and I have it in almost every sub and function I've made.
However, when it's run in a nested fashion, I wind up with a mixed bag by the end of the parent sub. For example:
Sub A (OPTIMIZE_On), call Sub B
Sub B (OPTIMIZE_On), call Sub C
Sub C (OPTIMIZE_On), (OPTIMIZE_Off), Return to Sub B
Sub B (OPTIMIZE_Off), Return to Sub A
Sub A (OPTIMIZE_Off)
This means that everything after Sub C returns to Sub B is enabled (OPTIMIZE_Off).
My desire is to have a switch of some type where by any call to (OPTIMIZE_) that is NOT from the Parent Sub is ignored. My first thought was to have the (OPTIMIZE_) depend on the procedure name (rtsProcName) but I'm at a loss on how to make the sub recognize the first call, set some switch, and then test the current sub name against the parent sub; if FALSE then Quit Sub.
Any thoughts??? Thanks y'all
However, when it's run in a nested fashion, I wind up with a mixed bag by the end of the parent sub. For example:
Sub A (OPTIMIZE_On), call Sub B
Sub B (OPTIMIZE_On), call Sub C
Sub C (OPTIMIZE_On), (OPTIMIZE_Off), Return to Sub B
Sub B (OPTIMIZE_Off), Return to Sub A
Sub A (OPTIMIZE_Off)
This means that everything after Sub C returns to Sub B is enabled (OPTIMIZE_Off).
My desire is to have a switch of some type where by any call to (OPTIMIZE_) that is NOT from the Parent Sub is ignored. My first thought was to have the (OPTIMIZE_) depend on the procedure name (rtsProcName) but I'm at a loss on how to make the sub recognize the first call, set some switch, and then test the current sub name against the parent sub; if FALSE then Quit Sub.
Any thoughts??? Thanks y'all
Code:
Public gbl_nlbOptimize As Boolean ' ~~ Switch to tell subroutines NOT to run OptimizeVBA, but to only run from main routine
Public Sub OptimizeVBA(isOn As Boolean, _
rtsProcName As String)
' ~~ Procedure for increasing VBA performance
' [URL]http://analystcave.com/excel-improve-vba-performance/[/URL]
' Excel VBA Performance Coding Best Practices: Turn off some Excel functionality so your code runs faster
' [URL]http://blogs.office.com/2009/03/12/excel-vba-performance-coding-best-practices/[/URL]
' With Application: [URL]https://msdn.microsoft.com/en-us/library/office/ff835544.aspx[/URL]
On Error Resume Next
With Application
.ScreenUpdating = Not (isOn)
.EnableEvents = Not (isOn)
.DisplayAlerts = Not (isOn)
' .DisplayStatusBar = Not (isOn)
.PrintCommunication = Not (isOn)
.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
End With
ActiveSheet.DisplayPageBreaks = Not (isOn)
On Error GoTo 0
End Sub