我想在用户单击Excel工作表中的单元格后显示一个对话框。 像这样:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox "a cell was clicked!", vbOKOnly, "a click"
End Sub
它完美无缺。问题是,在双击编辑模式打开后,预计会输入公式。如何禁用此行为?
我想实现纯粹的功能: 〜用户点击一个单元格 〜出现一个对话框 〜用户关闭对话框 〜一个单元格不进入编辑模式,该表格看起来与双击事件之前完全一样。
答案 0 :(得分:14)
您必须使用参数中给出的变量取消操作:
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
这是一个虚拟的例子:
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
请注意,您不必将Cancel
设置为False
,因为它是默认值(这是出于示例目的)。