Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit / disable edit mode after double click handled event

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.

like image 214
wilu Avatar asked Jan 13 '12 09:01

wilu


1 Answers

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

like image 69
JMax Avatar answered Oct 31 '22 14:10

JMax