Screen Updating

It can likewise be annoying to see the screen change and flicker while a macro is running. This happens with macros that select or activate objects and is typical of the code generated by the macro recorder.

It is better to avoid selecting objects in VBA. It is seldom necessary to do this, and your code will run faster if you can avoid selecting or activating objects. Most of the code in this book avoids selecting where possible.

If you want to freeze the screen while your macro runs, you use the following line of code:

Application.ScreenUpdating = False

The screen remains frozen until you assign the property a value of True, or when your macro finishes executing and returns control to the user interface. There is no need to restore ScreenUpdating to True, unless you want to display screen changes while your macro is still running.

There is one situation where it is a good idea to set ScreenUpdating to True while your macro is running. If you display a user form or built-in dialog box while your macro is running, you should make sure screen updating is on before showing the object. If screen updating is off and the user drags the user form around the screen, the user form will act as an eraser on the screen behind it. You can turn screen updating off again after showing the object.

A beneficial side effect of turning off screen updating is that your code runs faster. It will even speed up code that avoids selecting objects, where little screen updating is required. Your code runs at maximum speed when you avoid selecting and turn off screen updating.

0 0

Post a comment