Determine when Enter Key is pressed in a sheet??

S

Subodh

I want to determine when Enter key is pressed in a sheet that is
active??
I would like then to run a macro when the Enter key is pressed ??
 
P

Paul C

The Enter Key is not a capturable event.

Worksheet change is
This added to the individual worksheet in VBA will trigger when ever the
sheet is changed.

Private Sub Worksheet_Change(ByVal Target as Range)
'Do Stuff
End Sub

If you are going to make changes to the sheet using this event is is usually
a good idea to turn off events at the start and then turn them back on at the
end. Otherwise the changes trigger this macro again.

Private Sub Worksheet_Change(ByVal Target as Range)
Application.EnableEvents=False
'Do Stuff
Application.EnableEvents=True
End Sub
 
H

Hubisan

It is possible with application.onkey. Example is with HOME-Key, for
other keys check VBA help inside Excel or look for it on the web:

Sub StartOnKey()
'Run this sub to make the HOME-key run the "SubHello" procedure
Application.OnKey "{HOME}", "SubHello"
End Sub

Sub SubHello()
'Pressing HOME will run this procedure
MsgBox "hallo"
End Sub

Sub StopOnKey()
'Run this sub to return HOME-Key to its normal meaning.
Application.OnKey "{HOME}"
End Sub

You can use Workbook_Open() and run application.onkey to bind a
procedure to a key as soon as the workbook is opened.

Hubisan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top