Home » excel » excel – Avoid selection in a form when double-click

excel – Avoid selection in a form when double-click

Posted by: admin May 14, 2020 Leave a comment

Questions:

In I have a macro that when double clicking a cell, a form will open.

When the relevant cell is located within the range in which the form will appear, an undesired selection is carried out in the form.

How can I avoid this selection?


From a suggested edit by hammejdp on barrowc’s answer:

I use this (i.e. BeforeDoubleClick event) already but not fix the problem

Private Sub Workbook_SheetBeforeDoubleClick(ByVal sh As Object, ByVal target As Range, cancel As Boolean)

Call s_Click_DoubleClick(sh, target, cancel)

End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal sh As Object, ByVal target As Range, cancel As Boolean)

Call s_Click_DoubleClick(sh, target, cancel)

End Sub

Private Sub s_Click_DoubleClick(sh, target, cancel)

Application.ScreenUpdating = False

If sh.Name <> "Legende" Then
    cancel = True
    ' Maak gebruik van een range
    vRowCount = target.Rows.Count
    vColumnCount = target.Columns.Count
    f_Input.TextBox1.Value = vColumnCount
How to&Answers:

If you are using the BeforeDoubleClick event, try setting Cancel to True within the event handler

Answer:

Setting Cancel = True does not resolve the issue. To replicate the error, create a User Form with a List Box and set the List Box Row Source to a range of cells. In a worksheet’s BeforeDoubleClick event, insert code to launch the form (e.g. frmMyform.Show). Now double-click somewhere in the center of the worksheet, in the area on the screen where the form will appear. What will happen is the second click of your double-click will be interpretted as trying to make a selection on the form.

Here is another way to replicate the problem. Do the same as above, but during your double click HOLD DOWN the second click. As you will see, your user form appears before you even release the second click of the double-click. While holding down the mouse, you can drag the mouse around and see that when you let go of the second click, the mouse actually selects the item in the list box where the mouse pointer is located.

To summarize, the intention of the poster is that double-clicking in a cell will launch a form. The effect is that it does indeed launch the form, BUT ALSO interprets the second click in the double-click as also a click on that form. Setting Cancel = True does not resolve the issue.

Answer:

Setting Cancel = True will not work. Here is some code that will resolve the issue. It’s not pretty, but it works. First, disable the ListBox when the user form initializes:

Private Sub UserForm_Initialize()
    Me.listMyList.Enabled = False
End Sub

This will prevent the second click from the BeforeDoubleClick event from being interpreted as a selection in the List box on the user form (or rather, it will block the ability for the second click to be interpreted as such because the control is not Enabled). Next, enable the ListBox on the user form’s MouseDown event:

Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Me.listMyList.Enabled = True
End Sub

It’s not pretty, but it works.

Answer:

The best solution I found so far is putting a small delay in the initialize routine of the form’s code as such:

Private Sub UserForm_Initialize()

Application.Wait (Now + #12:00:01 AM#)

End Sub

The minimum increment of time seems to be 1 second. If anyone has a cleaner solution or is able to reduce the delay to less than 1 second, kindly share. Note that the “Sleep” function does not work here in preventing the form click (Public Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)).