Application.EnableCancelKey = xlErrorHandler not working

sparky2205

Active Member
Joined
Feb 6, 2013
Messages
480
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a macro to delete worksheets.
The first step is to select the worksheet name.
The user is presented with a popup list of worksheet names
At this point I want to give the user the option to press Esc and quit the operation.
This is how I had hoped to achieve this (partial code):
VBA Code:
Sub Delete_Worksheet()
On Error GoTo errhandler
Application.EnableCancelKey = xlErrorHandler
Application.CommandBars("Workbook tabs").ShowPopup
[B].
.
.
.[/B]
errhandler:
    End
End Sub

The problem is
VBA Code:
Application.EnableCancelKey = xlErrorHandler
doesn't appear to work as after the Esc key is pressed the code continues to execute i.e. it does not jump to errhandler.
Hopefully I'm making some basic mistake that you can point out to me.
Note: Ctrl+Break does work.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I got this sorted. The code below is partial but these are the important bits.
Through a mix of Option Explicit, Application.EnableCancelKey = xlErrorHandler, errhandler and the ThisWorkbook.Protect.
I'll admit I don't fully understand the solution I've come to but it does work.
At one point execution was alternately protecting and unprotecting my worksheet. I overcame this by playing around with the ThisWorkbook.Protect, which is why there might appear to be more of those statements than necessary.
I'm posting this as a solution to my initial problem in case it may be of use to someone else.
VBA Code:
Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
#Else
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
#End If

On Error GoTo errhandler
Application.EnableCancelKey = xlErrorHandler
Application.CommandBars("Workbook tabs").ShowPopup
If CBool(GetKeyState(VBA.vbKeyEscape) And &H10000) Then
        ThisWorkbook.Protect Password:=pw
        Application.DisplayAlerts = True
        GoTo errhandler
End If
VBA Code:
Exit Sub

errhandler:
    If Err.Number = 18 Then
        ThisWorkbook.Protect Password:=pw
        Application.DisplayAlerts = True
    End If
    ThisWorkbook.Protect Password:=pw
    Application.DisplayAlerts = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,093
Messages
6,123,068
Members
449,091
Latest member
remmuS24

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