+ Reply to Thread
Results 1 to 7 of 7

Run code on keypress enter

  1. #1
    Registered User
    Join Date
    07-12-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    3

    Run code on keypress enter

    I want to run a simple macro every time someone presses the enter key. I have been trying to do this for a while and I have found examples that should work but non of them seem to do a damn thing. I haven't written macros for excel before so I am guessing that I am missing something fundamental about where I should put my code or something.

    Any help greatly appreciated.

    Cheers

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Run code on keypress enter

    Hello adenjones,

    My guess is Vapor Ware! I don't see line one of code or a workbook. What was the question?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Run code on keypress enter

    You are nor very specific in why you want this, whether it's about entering enter in a cell, a control in a userform, an active-X- control, etc.
    Please be more precise and the answers will flow....

  4. #4
    Registered User
    Join Date
    07-12-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Run code on keypress enter

    I need the code to run every time someone presses enter in a cell in a worksheet.

    I have been trying something similar to this:
    Please Login or Register  to view this content.
    But nothing at all happens.
    Last edited by Leith Ross; 07-12-2010 at 12:30 PM. Reason: Added Code Tags

  5. #5
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Run code on keypress enter

    Hi adenjones;

    KeyDown and KeyUp only work on objects in a form. There is no equivalent (that I can find) for a spreadsheet. The 2 events that come the closest are
    "Private Sub Worksheet_Change(ByVal Target As Range)", but it can't tell whether the user changed the cell or another macro changed the cell
    and
    "Private Sub Worksheet_SelectionChange(ByVal Target As Range)", but it can't tell if the user changed selection with an enter key, arrow key, or if a macro changed the selection,
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  6. #6
    Registered User
    Join Date
    07-12-2010
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Run code on keypress enter

    Thanks for the reply. I actually ended up solving it with

    Private Sub Workbook_Open()
    Application.OnKey "~", "DoSomeThing"
    End Sub

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Run code on keypress enter

    Got to admit, I didn't think of that. Good Show.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1