14

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

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.

Community
  • 1
  • 1
wilu
  • 549
  • 1
  • 12
  • 26

1 Answers1

22

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 Cancel to False because it the default value (this is for the example purpose).

JMax
  • 26,109
  • 12
  • 69
  • 88