Trapping for or at worst disabling esc key during subs' execution

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
I want to either trap user hits esc key or if not that, disable it. I've tried what I can find with google. Sub 1 loops and calls Sub 2 within the loop.

I tried putting Application.EnableCancelKey = xLDisabled into both subs just to disable the esc key which did not work. When user hits esc I get an overload (error # 6) and the dialog with the debug button takes me to my error handler function call (to function ErrorMessage shown below). I don't want user to see that.

I tried trapping for error # 18 in error handler but I still get an overload (error # 6) message and debug button takes me to the error handler function call (to function ErrorMessage shown below).

Here is my attempt at tapping error # 18:

VBA Code:
ErrHandler:

CentersProgressDialog.Hide
DoEvents

If Err.Number = 18 _
 Then
    Exit Sub
Else
    Call ErrorMessage(Err.Number, Err.Description, sSubName, sStepID)
End If

End Sub

Here is the error handler function which normally works.

VBA Code:
'Use to render the error messages from error handler.
Function ErrorMessage( _
    pErrNum As Integer, _
    psErrDescr, _
    Optional psSubName = "", _
    Optional psStepID = "")
    
'    If pErrNum = 18 Then Exit Function

    Dim sMsg As String
        
    Dim sTitle As String
    
    sTitle = "Error Message"
    
    sMsg = "Error #" & pErrNum & " occurred"
    
    If psSubName <> "" _
     Then sMsg = sMsg & Chr(10) & "in procedure " & psSubName
    
    sMsg = sMsg & "."
    
    If psStepID <> "" _
     Then sMsg = sMsg & Chr(10) & "Step ID: " & psStepID & "."
    
    sMsg = sMsg & Chr(10) & "Error Type: " & psErrDescr & "."
    
    MsgBox sMsg, vbOKOnly + vbCritical, sTitle
    
    Err.Clear
    
    Application.StatusBar = False
    DoEvents

End Function

What simple code am I missing?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Did you preceed the EnableCancelKey command with On Error GoTo ErrHandler ?
Also, is the code in a userform module ?
 
Upvote 0
Jaafar

Thanks for your assistance!

This code is in two normal modules, not in a form.

I do have a reference to my error handler at the very beginning of both subs.

Excel Formula:
'   ----------------------
'       Error Handling
'   ----------------------
    Dim sSubName As String
    Dim sStepID As String

    sSubName = "Create_SSCD_CS_IRAA_Workbook"
    sStepID = ""
    On Error GoTo ErrHandler
'   ----------------------

I realize that my error handing is probably primitive/amateurish but it does what I want.
 
Upvote 0

Forum statistics

Threads
1,224,746
Messages
6,180,705
Members
452,994
Latest member
Janick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top