Exit / disable edit mode after double click handled event

wilu picture wilu · Jan 13, 2012 · Viewed 24.3k times · Source

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.

Answer

JMax picture JMax · Jan 13, 2012

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).