Prevent Escape on Nonmodal Userform with Progressbar

L

Lazzaroni

Is there any way to use a progressbar control in a modal userform?

I need to block users from accessing Excel or interrupting code execution by
pressing Escape
while a progressbar is being displayed.

Pressing Escape while a nonmodal userform is active returns “Code execution
has been interrupted.†Pressing Escape while a modal userform is active does
the same thing as the Cancel button if a Cancel button control is present.

I tried capturing the keydown event with the following code, but the code
gets interrupted before it can capture the keycode:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = vbKeyEscape Then Unload Me
End Sub

Either I need to be able to display a progressbar control in a modal
userform, or I need to be able to capture the Escape KeyDown event.

Thanks for your help.
 
A

AndyM

Have you tried using the following line of code?
Application.EnableCancelKey = xlDisabled

This stops Ctrl+Break from interrupting code execution and I think it will
work in your case as well.

Andy
 
L

Lazzaroni

Andy:

Private Sub UserForm_Activate()
With Application
.Interactive = False
.EnableCancelKey = xlDisabled
End With
End Sub

The above code approximates a modal form, which is what I needed.

I would have liked to allow the user to use the Escape key in addition to
being able to click on the Cancel button, but EnableCancelKey = xlDisabled
prevents me from trapping the keycode as well, so the following code does not
work.

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = vbKeyEscape Then Unload Me
End Sub

I will try looking for a EnableCancelKey = xlErrorHandler workaround, but
ideally, I would like to run my user form with ShowModal = True.
Unfortunately, I haven’t found a way to display a progressbar control
correctly in a modal userform.

For the time being, this does the trick. Thank you for your suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top