简体   繁体   English

Excel VBA清除活动单元格

[英]Excel VBA Clear Active Cell

So I am needing to do a few things, the first is run a macro when the delete key is pressed, so I found this: 因此,我需要做一些事情,首先是在按下Delete键时运行宏,因此我发现了这一点:

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

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

Now according to what I read, this will in turn make the delete key useless so I also need it to clear the currently active cell at the same time, I found this: 现在,根据我阅读的内容,这将依次使删除键无效,因此我还需要它同时清除当前活动的单元格,我发现:

Sub SetValue()
Worksheets("Sheet1").Activate
ActiveCell.Value = 35
End Sub

and this: 和这个:

Selection.Clear

However, I am a new to VBA so I don't really know how to combine these to use the clear function on delete :).. Any help would be greatly appreciated, thank you. 但是,我是VBA的新手,所以我真的不知道如何将它们结合起来以在delete :)上使用clear函数。任何帮助将不胜感激,谢谢。

To set an active cell value, you can use this function in your module: 要设置活动单元格值,可以在模块中使用此功能:

Sub SetValue()
  ActiveCell = Null ' to clear the contents
End Sub

or you can use your code from the OP. 或者您可以在OP中使用您的代码。

Worksheet_Activate() and Worksheet_Deactivate are Workbook functions. Worksheet_Activate()Worksheet_DeactivateWorkbook函数。 In your VBAProject, double-click ThisWorkbook and change "General" to "Workbook" in the dropdown on top. 在您的VBAProject中,双击ThisWorkbook ,然后在顶部的下拉菜单中将“常规”更改为“工作簿”。 Then, in the drop-down on the right, you can select SheetActivate and SheetDeativate . 然后,在右侧的下拉列表中,可以选择SheetActivateSheetDeativate Add these functions and fill out with your code to disable DELETE key on your Sheet1 worksheet: 添加以下功能并填写您的代码以在Sheet1工作表上禁用DELETE键:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   If Sh.Name = "Sheet1" Then Application.OnKey "{DELETE}", "SetValue"
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
' This will enable DELETE
  If Sh.Name = "Sheet1" Then Application.OnKey "{DELETE}"
End Sub

To disable any processing when DELETE is pressed, specify "" as a 2nd argument to OnKey . 要在按下DELETE时禁用任何处理,请将""指定为OnKey的第二个参数。 And only pass the key name without the 2nd argument to enable processing (see MSDN reference ). 并且仅传递不带第二个参数的键名称以启用处理(请参阅MSDN参考 )。

PS This might trigger only after switching to the worksheet being handled. PS仅在切换到正在处理的工作表后才可能触发。

the second part of OnKey call "Intercept" is the name of the sub you have to add in a regular module. OnKey调用“ Intercept”的第二部分是您必须在常规模块中添加的子名称。

you just have to right-click on Modules > insert > module and add the sub like this : 您只需要右键单击“模块”>“插入”>“模块”,然后添加如下所示的子项:

Worksheet code : 工作表代码:

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

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

Module code : 模块代码:

Sub deleteAction()
    Selection.Clear
    [... the few things you have to do :) ...]
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM