I just want to underline that such behavior may happen. Refer to:
Why does VBA code run faster when called from a standard module (instead of a userform)?
Excel VBA code execution dramatically faster after breaking and resumiing
I have a vast code and it may contain a lot of “not so nice“ code sections. It has hundreds of sheets and all sheets containing only data but no calculations.
Very nice appearance: No screen flickering, most time <ScreenUpdating.false>, only one sheet shows info about jobs done, one progress bar with <ProgressWindow.Show>.
The program does its job in an industrial company but some report may take several hours.
From Time to time I try to speed up some sections. I tried all kind of tips and tricks. Subsequently I come across the same behavior and made me lose some hours.
One of the following interactions speeded up the processes:
- Esc and continue the code
- Change to another application
Finally I made two option buttons:
- “info” off: ___ sheet with info about jobs done
- “bar” off: ____ progress bar
run time | “info” | “bar” | interaction |
---|---|---|---|
100% | on | on | -- |
50% | off | off | -- |
25% | off | on | -- |
10% | off | off | either one |
10% | off | on | either one |
Does someone have ideas/suggestions:
- Why is the program running faster when progress bar is shown? (IsProgressBarOff=False)
- Why do such interactions speed up the program?
- How could I make advantage in the code of these interactions?
Sub ShowProgressBar(info)
If IsProgressBarOff Then Exit Sub
'activate progress bar
ProgressWindow.FullBar.Width = 0
'progress text
ProgressWindow.ProgressInfo.Caption = ""
'progress titel
ProgressWindow.Caption = info
ProgressWindow.Show
’positioning progress window
ProgressWindow.Top = Application.Top + (StartUpWindow.Height / 3) + 115
ProgressWindow.Left = (Application.Left + 280)
End Sub
Information added after comment of @CLR
Sub Sample
some code
ShowProgressBar Loop1
nr = 1
For Each s In Application.Sheets
SetProgressBar_A nr s.Name
some code 'which will take some seconds
nr = nr + 1
next s
End Sub
Sub SetProgressBar_A(incr, info)
If verifyVisual_IsProgressBarOff Then Exit Sub
'change bar
If incr > 100 Then incr = 100
If incr < 2 Then incr = 2
ProgressWindow.FullBar.Width = incr * 3
'show info
ProgressWindow.ProgressInfo.Caption = info
ProgressWindow.Repaint
DoEvents
End Sub