vba 双击处理事件后退出/禁用编辑模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8848253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:24:27  来源:igfitidea点击:

Exit / disable edit mode after double click handled event

excelexcel-vbaexcel-2007vba

提问by wilu

I'd like to display a dialog after a user clicks a cell in an Excel sheet. Something like this:

我想在用户单击 Excel 工作表中的单元格后显示一个对话框。像这样的东西:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    MsgBox "a cell was clicked!", vbOKOnly, "a click"
End Sub

It works perfectly fine. The problem is, after a double click edit mode is turned on and a formula is expected to be entered. How do I disable this behaviour?

它工作得很好。问题是,在打开双击编辑模式并期望输入公式后。如何禁用此行为?

I'd like to achieve pure functionality: ~ user clicks a cell ~ a dialog appears ~ user closes the dialog ~ a cell does NOT go into edit mode, the sheet looks exactly as it did before double click event.

我想实现纯粹的功能:~用户单击一个单元格~一个对话框出现~用户关闭对话框~一个单元格不会进入编辑模式,工作表看起来与双击事件之前完全一样。

回答by JMax

You have to cancel the action with the variable given in argument:

您必须使用参数中给出的变量取消操作:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
     MsgBox "a cell was clicked!", vbOKOnly, "a click"
     'Disable standard behavior     
     Cancel = True
End Sub

Here is a dummy example:

这是一个虚拟示例:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim response As Variant
response = MsgBox("Are you sure you want to edit the cell?", vbYesNo, "Check")
If response = vbYes Then
    Cancel = False
Else
    Cancel = True
End If
End Sub

Note that you wouldn't have to set Cancelto Falsebecause it the default value (this is for the example purpose).

请注意,您不必设置Cancel为,False因为它是默认值(这是出于示例目的)。