Forum Discussion
CHileman
Jul 01, 2022Copper Contributor
Help with Macro Hygiene Please
It's not that the macros run slowly, or that Excel crashes, but it just takes a while to get excel selected when coming from another program or webpage. I tend to have to cut and paste a good de...
- Jul 02, 2022
Try setting up your macro like this, it should actually be faster.
With Application .Calculation = xlCalculationManual .ScreenUpdating = False .EnableEvents = False End With 'Here insert your code With Application .Calculation = xlAutomatic .ScreenUpdating = True .EnableEvents = True End With
Example according to your code specifications.
'Example with your first code Sub Unhide_All() With Application .Calculation = xlCalculationManual .ScreenUpdating = False .EnableEvents = False End With If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then ActiveSheet.ShowAllData End If Columns.EntireColumn.Hidden = False Rows.EntireRow.Hidden = False With Application .Calculation = xlAutomatic .ScreenUpdating = True .EnableEvents = True End With End Sub
Hope I was able to help you :).
I know I don't know anything (Socrates)
NikolinoDE
Jul 08, 2022Gold Contributor
Try to explain 🙂 in a few words why these commands could be used.
Please also consider that I do not know the formula and VBA structure of your workbook.
Calculation:
If you change cells/columns, etc. with VBA, to which functions of other cells refer, a recalculation is always started.
Depending on the functions and the associated number of calculations, it can have an impact (mostly negative) on performance.
While Excel is working on the recalculation, the processing of the program code is slowed down because the program flow only continues when the calculation has been completed for the time being.
You can avoid this problem by switching off the automatic calculation in the initial code.
EnableEvents:
The Select methods in the macro would re-invoke the macro called by moving the cursor around the worksheet. Setting the EnableEvent property to False prevents this from happening.
Disabling screen updating speeds up your macro code. While you won't be able to see what the macro is doing, it will run much faster. The ScreenUpdating property should end up being set back to True when your macro exits.
Excel VBA reference (here you can find general information about VBA)
CHileman
Jul 08, 2022Copper Contributor
Thanks again NikolinoDE, all that makes perfect sense to me why it would help while running the macros - but why would it change anything at all for when the macros aren't being run? The only way to call them is to select them manually. Don't get me wrong, I love that they work! But I'm just baffled as to why they work.