PC Review


Reply
Thread Tools Rate Thread

code to execute on enter

 
 
=?Utf-8?B?c2M=?=
Guest
Posts: n/a
 
      19th Mar 2007
I am running excel 2002. I have a sheet with cell f5 active. This is the
only cell the user is allowed to type in or select. Is there a way to make
my vba code execute when the user presses the enter key?

Thanks
sc
 
Reply With Quote
 
 
 
 
okrob
Guest
Posts: n/a
 
      19th Mar 2007
On Mar 19, 11:44 am, sc <s...@discussions.microsoft.com> wrote:
> I am running excel 2002. I have a sheet with cell f5 active. This is the
> only cell the user is allowed to type in or select. Is there a way to make
> my vba code execute when the user presses the enter key?
>
> Thanks
> sc


Add a Worksheet_Change event to the worksheet code.
Chip Pearson has an excellent guide for beginners.
http://www.cpearson.com/excel/events.htm

But basically,

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$F$5" Then

'run your code here

End If
End Sub

 
Reply With Quote
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      19th Mar 2007
Hi sc -

Two addtional methods:

Method 1: Onkey method. Add the following two event procedures to the
worksheet's module (yourProcedure can be located in a standard module; it
will be called by the pressing of Return on the keyboard or Enter on the
keypad):

Private Sub Worksheet_Activate()
Application.OnKey "{Return}", "yourProcedure"
Application.OnKey "{Enter}", "yourProcedure"
End Sub

Private Sub Worksheet_Deactivate()
Application.OnKey "{Return}" 'resets the Return key
Application.OnKey "{Enter}" 'resets the Enter key
End Sub


Method 2: SelectionChange event. This procedure will work whether or not a
value is entered in F5.

First, unprotect the sheet, unlock the cell below F5, and then re-Protect
the sheet. Next, check |Tools | Options | EditTab and make sure that "Move
selection after Enter Direction" is set to 'Down.'
Finally, copy this procedure to the worksheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Range("F5").Select
Application.EnableEvents = True

'Your VBA code here

End Sub

Note: this second method doesn't require that the worksheet be protected at
all, but protection minimizes screen flicker associated with a user clicking
here and there to test the sheet.

--
Jay


"sc" wrote:

> I am running excel 2002. I have a sheet with cell f5 active. This is the
> only cell the user is allowed to type in or select. Is there a way to make
> my vba code execute when the user presses the enter key?
>
> Thanks
> sc

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute a macro from a dialog box on enter chenbear Microsoft Excel Programming 1 26th Nov 2008 04:03 AM
Execute command in field when enter key is pressed =?Utf-8?B?bW1hdHprZUBjb21jYXN0Lm5ldA==?= Microsoft Access Forms 2 31st Aug 2007 10:44 PM
Execute a command when ENTER is pressed in field =?Utf-8?B?bW1hdHprZUBjb21jYXN0Lm5ldA==?= Microsoft Access Forms 1 7th Jun 2007 03:52 PM
ENTER keys to execute code to add menu =?Utf-8?B?TWFyayBXYXJuZXI=?= Microsoft Excel Programming 1 25th Aug 2006 10:25 PM
Execute enter key on the client =?Utf-8?B?TWFyaw==?= Microsoft ASP .NET 1 10th Aug 2004 07:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:00 PM.