+ Reply to Thread
Results 1 to 12 of 12

Execute VBA Routine on Enter Key

  1. #1
    Forum Contributor rkjudy's Avatar
    Join Date
    03-31-2009
    Location
    Longview, TX
    MS-Off Ver
    MS Office 2010
    Posts
    239

    Execute VBA Routine on Enter Key

    Simple question. How do I have a workbook execute VBA code when I hit 'Enter' anywhere on a specific sheet? I don't need the code to execute when I hit 'Enter' on any of the other sheets in the workbook, just a specific sheet.

  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: Execute VBA Routine on Enter Key

    Hello rkjudy,

    It is possible to do. Because the Enter key is so vital to all Excel operations, it begs the question - Why? It would not be very efficient or practical from a coding standpoint. There may be a better solution but you have have to provide more details about what you're doing.
    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 Contributor rkjudy's Avatar
    Join Date
    03-31-2009
    Location
    Longview, TX
    MS-Off Ver
    MS Office 2010
    Posts
    239

    Re: Execute VBA Routine on Enter Key

    Thanks Leith. Here's what I'm trying to do. The workbook opens to a sheet with the focus on cell C14. The user is expected to put in a number (dollar amount) in this cell. After he enters the number and hits 'enter', I want to execute a routine that will perform a calculation based on THAT number and a number on another sheet and place the result in cell E21 on the original sheet.

    Basically, I can't get the answer I need until a number is placed in cell C14.

    Any alternatives would be appreciated.

  4. #4
    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: Execute VBA Routine on Enter Key

    Hello rkjudy,

    The value can be extracted from the cell after it is updated using the Worksheet_Change event. Each sheet has one of the event modules. Here is an example. This multiples C14 by the value in cell G5 on Sheet2 and stores the result in E21 of the active sheet.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor rkjudy's Avatar
    Join Date
    03-31-2009
    Location
    Longview, TX
    MS-Off Ver
    MS Office 2010
    Posts
    239

    Re: Execute VBA Routine on Enter Key

    Below is the code I entered, but nothing happens when I enter a number in cell C14.

    Please Login or Register  to view this content.
    BTW, the sheet name where this occurs is 'PriceWorkup'.

    What do you think I've done wrong? Also, will you tell me exactly where this code is supposed to go. I think I have it in the right place, but just to be sure.
    Last edited by rkjudy; 11-07-2009 at 12:06 AM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Execute VBA Routine on Enter Key

    The code is in a worksheet module (as it should be), not a code module or the ThisWorkbook module?

    Set a breakpoint on the first executable line and see if the code is being triggered.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Contributor rkjudy's Avatar
    Join Date
    03-31-2009
    Location
    Longview, TX
    MS-Off Ver
    MS Office 2010
    Posts
    239

    Re: Execute VBA Routine on Enter Key

    Nothing seems to work. FYI, I'm running Excel 2003. Do I need to turn something on the Object Library?

  8. #8
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Execute VBA Routine on Enter Key

    A side suggestion but you may not want to assign the macro to the enter key as Leith stated it is a vital keyboard function to Excel. Say you wanted to change a value or modify your formulas, everytime to hit the enter key it will run your macro instead of completing your initial task.

    You may be better assigning your macro to an object you can place on your spreadsheet next to the cell C14. For example insert a shape, say a rectangle, right next to cell C14 and put some text in it like "Go" or "Start" then right-click the shape and hit "Assign macro" and assign the macro you want to run to that shape. This way your enter key stays free and after entering the number in C14 you only have to mouse over to the rectangle and click on it to run your macro.

  9. #9
    Forum Contributor rkjudy's Avatar
    Join Date
    03-31-2009
    Location
    Longview, TX
    MS-Off Ver
    MS Office 2010
    Posts
    239

    Re: Execute VBA Routine on Enter Key

    That would be OK also. I was just trying to automate the macro execution without the extra step of hitting 'Go'. Thanks.

  10. #10
    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: Execute VBA Routine on Enter Key

    Hello rkjudy,

    If you post your workbook, I'll take a look at why it isn't working right.

  11. #11
    Forum Contributor rkjudy's Avatar
    Join Date
    03-31-2009
    Location
    Longview, TX
    MS-Off Ver
    MS Office 2010
    Posts
    239

    Re: Execute VBA Routine on Enter Key

    Let's try this. Let's forget the 'execute on enter' issue. How about execute a macro on change. I have a workbook that opens to a sheet with focus on cell C14 which is empty (null). I would like to have a macro execute if cell C14 changes in any way (ie. user enters a dollar amount). I would also like for the macro to re-execute if the value changes again (ie. the user enters a DIFFERENT different dollar amount). Any ideas on this?

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Execute VBA Routine on Enter Key

    would like to have a macro execute if cell C14 changes in any way
    That's what your code is set up to do now, provided that C14 is changed by manual entry (i.e., not the result of a formula it contains).

    The code MUST be in the appropriate sheet module, not a code module.

    Macros MUST be enabled. Set macro security to Medium and enable them when prompted.

+ 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