陷阱删除键 [英] Trap delete key
问题描述
我的问题相当琐碎:我需要将逻辑应用于Excel中的删除按钮.在一个相关问题中,我询问了清除数据透视表中单元格的方法,并且现在意识到这可能不是正确的方法,这是我正在考虑的另一种选择.不幸的是,我承认我对Visual Basic的经验很少,而且这项任务证明了这一挑战(非常令人惊讶).
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 that this might not be the right approach, this is yet another alternative I'm considering. Unfortunately, I admittedly have little experience with Visual Basic, and this task is proving such a challenge (quite surprisingly).
是否可以在按下Excel时捕获(如侦听)删除键并在VBA中触发子例程?
Is it possible to trap (as in listen to) the delete-key in Excel and trigger a subroutine in VBA when pressed?
我将不胜感激!谢谢!
推荐答案
是.
您需要使用Application.Onkey
为键分配代码
You case use Application.Onkey
to assign code to keys
如果 (1)将此代码添加到包含您的数据透视表的工作表模块中(右键单击工作表标签,查看代码并跳过下面的代码)
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)将此代码添加到常规模块中
(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
这篇关于陷阱删除键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!