I thought that setting ScreenUpdating and EnableEvents equal to false while executing code would disable all keyboard and mouse events while code is executing, but I was surprised to see that that was not the case.
I have created a simple example that takes a few seconds to execute. While it is executing, if you do anything with your mouse or keyboard, the action does not happen immediately but seems to queue up and execute after the code finishes executing.
For example, if I click cell D5 while the code is running and press delete, it will delete cell D5 immediately after my code finishes executing; likewise, clicking the Excel Close button while the code is executing will queue up a call to close Excel and will prompt me if I want to save before closing Excel.
Is there a way to disable all mouse and keyboard interaction with Excel whilst a macro is executing?
Here is the simple code I am using to test this:
I have created a simple example that takes a few seconds to execute. While it is executing, if you do anything with your mouse or keyboard, the action does not happen immediately but seems to queue up and execute after the code finishes executing.
For example, if I click cell D5 while the code is running and press delete, it will delete cell D5 immediately after my code finishes executing; likewise, clicking the Excel Close button while the code is executing will queue up a call to close Excel and will prompt me if I want to save before closing Excel.
Is there a way to disable all mouse and keyboard interaction with Excel whilst a macro is executing?
Here is the simple code I am using to test this:
Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Interactive = False
Application.Cursor = xlWait
Const Rows As Long = 100000
Const Cols As Long = 10
Dim MyArray(0 To Rows - 1, 0 To Cols - 1) As String
Dim i, j As Long
For i = 0 To Rows - 1
For j = 0 To Cols - 1
MyArray(i, j) = "String" & (i * Cols + j + 1)
Next j
Next i
Range(Cells(1, 1), Cells(Rows, Cols)).Value2 = MyArray
Application.Cursor = xlDefault
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Interactive = True
End Sub