Trap delete key

前端 未结 1 1853
迷失自我
迷失自我 2021-01-25 11:17

My problem is fairly trivial: I need to apply logic to the delete button in Excel. In a related question I asked for means to clear cells in a pivot table, and realising now tha

相关标签:
1条回答
  • 2021-01-25 11:37

    Yes.

    You case use Application.Onkey to assign code to keys

    If you (1) add this code to a sheet module that contains your pivottable (right-click your sheet tab, View Code and past the code below)

    This code activates and then de-activates the intercept when the user enters then leaves this particular sheet:

    Private Sub Worksheet_Activate()
    Application.OnKey "{DELETE}", "Intercept"
    End Sub
    
    Private Sub Worksheet_Deactivate()
    Application.OnKey "{DELETE}"
    End Sub
    

    (2) add this code to a regular module

    Sub Intercept()
    MsgBox "user just pressed delete"
    End Sub
    

    then delete on that sheet will be trapped and the user given a message

    0 讨论(0)
提交回复
热议问题