Lock Out User Actions
Sometimes you want to prevent users from interrupting Excel while you perform some time-consuming task in code. The Application object provides these ways to limit user interaction:
Set
DisplayAlertsto False to hide standard Excel dialogs while code runsSet
Interactiveto False to lock users out of Excel completelySet
ScreenUpdatingto False to hide changes as they are made by code
Each of these approaches should include some code at the end of the procedure to change the settings back to their defaults when your code finishes. Otherwise, you might lock a user out permanently!
The following code demonstrates how to lock out user actions temporarily while a long task executes:
Sub LockOutUser( )
Dim cel As Range
' Show the hourglass cursor.
Application.Cursor = xlWait
' Turn off user interaction, screen updates.
Application.Interactive = False
Application.ScreenUpdating = False
' Simulate a long task.
For Each cel In [a1:iv999]
cel.Select
Next
' Restore default settings.
Application.Interactive = True
Application.ScreenUpdating = True
Application.Cursor = xlDefault
[a1].Select
End SubOne of the side benefits of setting ScreenUpdating to False is that the preceding code executes more quickly since Excel doesn’t have to update the screen or scroll the worksheet as cells are selected. Again, just be sure to turn screen updates back on when done.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access