Event On Enter

ExHell

Board Regular
Joined
Aug 4, 2003
Messages
55
Does anyone no a way to trigger a macro by just using the enter key
in a difined rage ("A1:A100")
Not worksheetchange
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Does anyone no a way to trigger a macro by just using the enter key
in a difined rage ("A1:A100")
Not worksheetchange
I'm not quite sure what you mean by "Not worksheetchange", but wouldn't this do what you want?


<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(Target, Range("A1:A100")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">' Whatever code you want to run goes here (or macro name)</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
I diddn't wan't a worksheet_Change event
in some instance's the data wont be changed
i was looking at excuting a macro just by hitting the enter key
 
Upvote 0
I diddn't wan't a worksheet_Change event
in some instance's the data wont be changed
i was looking at excuting a macro just by hitting the enter key

Just a thought:

Could you not just place a Button on your sheet and assign your macro, (whatever that is) to the button. Then by hitting (clicking) the Button, the macro would run.

Harry
 
Upvote 0
Yes there is a way, but Why? There may be another approach that you do not know of.
 
Upvote 0
:confused:

In workbook class.
Code:
Option Explicit
 
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
 
Private Sub Workbook_Open()
    GetAsyncKeyState vbKeyReturn
End Sub

In the worksheet.
Code:
Option Explicit
 
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
        If GetAsyncKeyState(vbKeyReturn) Then
            MsgBox "Enter key pressed within range A1:A100"
        End If
    End If
End Sub

:p

<a href="http://home.fuse.net/tstom/0627082337.1608245.zip"><img src="http://home.fuse.net/tstom/zip.gif"width="48"height="48"border="0"></a> <a href="http://home.fuse.net/tstom/0627082337.1608245.zip">0627082337.1608245.zip</a>
 
Last edited by a moderator:
Upvote 0
How about putting Peter's code in the Selection_Change event, which triggers even if no value is changed?
One could use a static range variable and probe the Application.MoveAfterReturnDirection to determine if Enter was pressed, or the mouse used to click a cell.
 
Last edited:
Upvote 0
Being the Devil's advocate on this;

Keep in mind that normally when you (or your user) hit the enter key, you will move down to the next cell.

Yes there is a way, but Why? There may be another approach that you do not know of.

It might help to know what your macro is going to do with or without a change on the sheet.

Harry

Sorry: I didn't see all the "Pro's" comming on to this post. I think their replies are far better then my post. Just was trying to narrow down something.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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