VB for Enter Key and Double Click

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
Hi,

I have VB written that acts upon a double click preformed on a cell.

This works great. The question I have; is it possible to also act if an enter key is press on the same cell?

My code in the worksheet vb:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

'-------------------------------------------------------
'Hardware Master - Do on Double Click
'-------------------------------------------------------

Set theRange = Range("L4:L" & 4)

If Not Intersect(Target, theRange) Is Nothing Then

SheetName = Me.Name

UserForm1.Show

Endif

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
If you mean when you enter a value try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set theRange = Range("L4:L" & 4)
If Not Intersect(Target, theRange) Is Nothing Then
    SheetName = Me.Name
    UserForm1.Show
End If
End Sub
 
Upvote 0
Thanks VoG...

I want the user to be able to enter data into cell L4 then press enter or, double click cell L4 and a userform appears.

If the user enters information into L4 the presses the enter key, I do not want to launch the userform. If they double click L4, then I want to launch the userform.

With the user pressing the enter key after data is entered into cell L4, I will take a different path in my code than if they double clicked on cell L4.

Thanks again.
 
Upvote 0
In that case try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set theRange = Range("L4:L" & 4)
If Not Intersect(Target, theRange) Is Nothing Then
    '
    'do something
    '
End If
End Sub
 
Upvote 0
Sorry to be a pain VoG...

You may be giving me the answer I want but I don't get it just yet...

Here's what I'm lookng for:

Private Sub Worksheet_Change(ByVal Target As Range)
Set theRange = Range("L4:L" & 4)
If Not Intersect(Target, theRange) Is Nothing Then
If Enter Key pressed then
.
.
Do this
.
.
Else if Double Click
.
.
Do this
.
.
End If
End Sub

I guess I'm looking to act upon one of two events that can occur in the same worksheet.

Thanks again.
 
Upvote 0
So you need both pieces of code as separate subs within the worksheet's code module.

BeforeDoubleClick will run on double clicking.

Worksheet_Change will run on pressing Enter.
 
Last edited:
Upvote 0
It will be 2 different codes.

KEEP the code you already have that you originally posted.

In ADDITION to that code, put in the code Vog posted.
replace the line
'do something
with whatever it is you want to do when Enter is pressed.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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