The following example demonstrates a procedure that requires a long period of time to complete. If a user interrupts the procedure, an error is caught. The interruption of the user first confirms that the proceedings must indeed be interrupted and then exit the procedure smoothly. So have a look at an example :
Code:
Sub ProcessData ()
'Set up a user interrupt trapping as a run-time error
On Error GoTo UserInterrupt
Application.EnableCancelKey = xlErrorHandler
'Start a long duration task
For x = 1 to 1000000
For y = 1 to 10
Next y
Next x
Exit Sub
UserInterrupt:
If Err = 18 Then
If MsgBox ( "Stop processing records?", VbYesNo) = vbNo Then
'Keep running at the point procedure was interrupted
Resume
Else
'Handle other errors that occur
MsgBox Error (Err)
End If
End If
End Sub
Bookmarks