How can I define a range to detect a Double Click?

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
I have used this subroutine to define an entire worksheet several times.

Code:
Private Sub worksheet_beforedoubleclick(ByVal Target As Range, Cancel As Boolean)

But I would like to be more surgical and specify specific cells. I need to detect a double click F2, F4, F6, F8 and F10, each of these cells has a border around it. If it is easier I could include everything from F2 to F10.

How can I define the range to detect a double click?

Thank you,
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I have used this subroutine to define an entire worksheet several times.

Code:
Private Sub worksheet_beforedoubleclick(ByVal Target As Range, Cancel As Boolean)

But I would like to be more surgical and specify specific cells. I need to detect a double click F2, F4, F6, F8 and F10, each of these cells has a border around it. If it is easier I could include everything from F2 to F10.

How can I define the range to detect a double click?
Try it like this...

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  If Not Intersect(Target, Range("F2,F4,F6,F8,F10")) Is Nothing Then
    Cancel = True
    MsgBox "Do what you want at this location"
  End If
End Sub
 
Upvote 0
Thank you Rick,

I have never used Intersect before, now I can add it to my arsenal of tools!

Thank you!
 
Upvote 0
Hey Rick, same question different problem.
Without the Intersect line you provided I double click and it performs the function without accessing the cell. But with this line, when I double click the cursor is in the cell waiting for the user to type. I want the cell selected but not open for editing.

In case I am not being clear it is like pressing F2 on the cell after double clicking. Any suggestions?
 
Upvote 0
Good morning Rick, I hope you had a good weekend and I'm sorry to repeat my question but the program is not asking as it should.

Without the Intersect line you provided I double click and it performs the function without accessing the cell. But with this line, when I double click the cursor is in the cell waiting for the user to type. I want the cell selected but not open for editing.

In case I am not being clear it is like pressing F2 on the cell after double clicking. Any suggestions?

Thank you,
 
Upvote 0
Good morning Rick, I hope you had a good weekend and I'm sorry to repeat my question but the program is not asking as it should.

Without the Intersect line you provided I double click and it performs the function without accessing the cell. But with this line, when I double click the cursor is in the cell waiting for the user to type. I want the cell selected but not open for editing.
I am not sure what you are trying to describe to me. The "Intersect line" controls whether the code between the "If..Then" and the "End If" will be executed or not. To me, this is what the "Intersect line" is...

Code:
If Not Intersect(Target, Range("F2,F4,F6,F8,F10")) Is Nothing Then

You cannot remove it without removing the "End If" also, or you will get an error. The way the "Intersect line" works is this. Target is the cell that was double-clicked... and, because you can only double-click one cell at a time, Target is guaranteed to be a single cell reference. The Intersect function returns the address of the cell when that cell is either F2, F4, F6, F8 or F10 and the reference is Nothing otherwise. So, if the result of the intersect is "not nothing", then it must be because it returned an address which means Target is one of the cells F2, F4, F6, F8 or F10; hence, the code between the "If..Then" and the "End If" executes. The first line of that code is "Cancel=True"... that tells VB not to react physically to the double-click... that means, it is told not to enter Edit mode (what happens when the F2 control key is pressed). The other line of code between the "If..Then" and the "End If" displays your MessageBox. Hopefully that explanation will lead you to whatever it is you are attempting to do. If not, you are going to have to clarify your question for us.
 
Upvote 0
Thank you Rick you did it again,

I have always had Cancel = True in my code for the worksheet_beforedoubleclick and you had it in the code you provided me but somehow it got lost. When I put it back in it worked as expected.

My hat is off to you!
 
Upvote 0

Forum statistics

Threads
1,215,943
Messages
6,127,814
Members
449,409
Latest member
katiecolorado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top