Modify existing code that disables the delete and backspace key

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please can someone help me?

I have found the following code that disables the delete and backspace keys to all my sheets apart from the last sheet.

I was wondering if anyone knows how to modify it so it only works on sheet3 or better still limiting it to the range of D40:BM40 on sheet3?

Here is the code:

Place following code in STANDARD module:

Code:
[COLOR=#333333]Sub SetOnkey(ByVal state As Integer)[/COLOR]    'adapted from solution developed by
    'DMT32 aka Dave Timms & Jerry Sullivan MVP
    If state = xlOn Then
        With Application
            .OnKey "{DEL}", "'AlertUser ""Delete""'"             'Delete Key
            .OnKey "{BACKSPACE}", "'AlertUser ""BackSpace""'"    'BACKSPACE Key
        End With
    Else
        'reset keys
        With Application
            .OnKey "{DEL}"
            .OnKey "{BACKSPACE}"
        End With
    End If
End Sub




Public Sub AlertUser(ByVal Button As String)
    MsgBox "you pushed the " & Button & " button", 48, "Function Disabled"
[COLOR=#333333]End Sub[/COLOR]


Place following code in the THISWORKBOOK code page:

Code:
[COLOR=#333333]Private Sub Workbook_SheetActivate(ByVal Sh As Object)[/COLOR] 
If Sh.Index <> Worksheets(Worksheets.Count).Index Then SetOnkey xlOn
End Sub



Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    If Sh.Index <> Worksheets(Worksheets.Count).Index Then SetOnkey xlOff
End Sub


[COLOR=#333333]Place following code in the THISWORKBOOK code page[/COLOR]

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    If ActiveSheet.Index <> Worksheets(Worksheets.Count).Index Then SetOnkey xlOn
End Sub
 
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    SetOnkey xlOff [COLOR=#333333]End Sub[/COLOR]

Any help would be much appreciated.

Thanks

Dan
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You don't need to change the onkey routine, but get rid of all the workbook event routines and then just put these two into worksheet 3
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Not Intersect(Target, Range("D40:BM40")) Is Nothing Then
   SetOnkey xlOn
 Else
    SetOnkey xlOff
 End If


End Sub


Private Sub Worksheet_Deactivate()
    SetOnkey xlOff
End Sub
 
Upvote 0
Hi offthelip,

Thank you for your help and it's working perfectly.

Thanks
Dan
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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