Forum Discussion
Help with Macro Hygiene Please
- Jul 01, 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 WithExample 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 SubHope I was able to help you :).
I know I don't know anything (Socrates)
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)