What is an User interrupt handling in VBA of Excel?
Hello friends,
I have recently started with the Visual Basic for Applications in an Excel. I want to know about an interrupt handling that takes place in VBA. So thought that there would be someone hanging out there who can help me.!! Please tell me what is an User interrupt handling in VBA of Excel? Since I don't know much about it, please provide information in details.
Re: What is an User interrupt handling in VBA of Excel?
When you add code handling errors in your macros to Visual Basic applications, you discover that the same mistakes are treated repeatedly. You can reduce the size of your code and effort required to write code by writing a few procedures that your code error handling can call to handle situations common error. A user can interrupt a Visual Basic for applications by pressing Command-period. It is possible to disable interrupts for procedures in your applications finished. However, if you do not disable the user interrupts the procedure is complete, you can ensure that your procedure is notified when an interruption occurred so that it can close files, disconnect shared resources or restore modified variables before returning control to the application to the user.
Re: What is an User interrupt handling in VBA of Excel?
You can trap user interrupts in your procedures by setting the property EnableCancelKey on xlErrorHandler. When this property is set, all interrupts generates an error number 18, which can be intercepted using an On Error executing. You can handle the error to stop the procedure and exit the program. If the Resume statement is used in the procedure following a runtime error intercepted, the interrupt is ignored.
Re: What is an User interrupt handling in VBA of Excel?
It is also possible for you to ignore user interrupts completely by setting the property EnableCancelKey on xlDisabled. In this report, Microsoft Excel for Mac ignores all attempts by the user to interrupt the process running. To restore the default interrupt processing, change the property setting EnableCancelKey to xlInterrupt. To prevent a procedure permanently disable interrupts from the user, Excel for Mac always restore the default property EnableCancelKey xlInterrupt whenever the procedure completes execution. To ensure that interrupts are handled correctly in your code must explicitly disable or trap the interrupts each time the procedure is executed.
Re: What is an User interrupt handling in VBA of Excel?
You must also keep in mind that interrupt handler can only be used for each procedure and the same manager is used for all errors encountered by implementing the scheme. If you run the macro ProcessData and then quickly press CTRL + Break, a message box asking if you want to stop processing records appears. If you click Yes, another message box with "User interrupt occurred" appears. If you click OK in the message box, the macro ends. If you click No in the first message box, the macro continues. The Resume statement resumes code execution after a routine error handling is complete.
Re: What is an User interrupt handling in VBA of Excel?
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