If your form automatically loads when Excel (or the particular workbook)
opens, you need to prevent that from happening if you want to edit your
code. When you open the workbook, hold down the SHIFT key. That will prevent
workbook startup code from running. Now you'll have your workbook open but
will not have the start up form visible. Edit your code normally.
I don't think you can trap the CTRL+BREAK key if the procedure that shows
the form terminates. CTRL+BREAK doesn't make it through to the usual
KeyPress, KeyUp, and KeyDown events of the UserForm object. One way to do it
would be to rewrite the procedure that displays the form as follows:
Sub ShowTheForm()
Dim N As Long '<< Test only. Remove.
Dim Done As Boolean
Const BREAK_KEY_PRESSED = 18
On Error GoTo BreakKeyHandler:
'''''''''''''''''''''''''''''''''''''''''''''
' Configure EnableCancelKey to raise an error
' 18 when the user types CTRL+BREAK.
'''''''''''''''''''''''''''''''''''''''''''''
Application.EnableCancelKey = xlErrorHandler
'''''''''''''''''''''''''''''''''''''''''''''
' Show The Form modelessly
'''''''''''''''''''''''''''''''''''''''''''''
UserForm1.Show vbModeless
'''''''''''''''''''''''''''''''''''''''''''''
' Loop doing nothing. Worksheet and form
' events will continue to work as expected.
'''''''''''''''''''''''''''''''''''''''''''''
Done = False
Do Until Done = True
N = N + 1 ' <<< Testing: Omit this line
DoEvents ' Release to other threads
Loop
''''''''''''''''''''''
' Get out.
''''''''''''''''''''''
Exit Sub
BreakKeyHandler:
Done = True
''''''''''''''''''''''''''''''''''''''''''
' If the user pressed CTRL+BREAK, VBA will
' raise an error 18.
''''''''''''''''''''''''''''''''''''''''''
If Err.Number = BREAK_KEY_PRESSED Then
''''''''''''''''''''''''''''''''''''''
' User type CTRL+BREAK. Hide the
' form.
''''''''''''''''''''''''''''''''''''''
MsgBox "User typed CTRL+BREAK" '<<< Testing Only. Remove
UserForm1.Hide
' Unload UserForm1
Exit Sub
Else
'''''''''''''''''''''''''''''''''''''
' Some other error occurred.
'''''''''''''''''''''''''''''''''''''
MsgBox "An unexpected error occurred: " & _
CStr(Err.Number) & " Description: " & Err.Description
End If
End Sub
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)