+ Reply to Thread
Results 1 to 15 of 15

VBA to move cursor when ENTER is pressed

  1. #1
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    VBA to move cursor when ENTER is pressed

    I have seen multiple threads with VBA code for moving the cursor, however, all seem to be based on data being entered in a cell. I have a form that sometimes data will be entered in the cell and sometimes the cell will be left blank. I need the cursor to move from cell to cell when ENTER is pressed whether data has been entered or not.

    Also, sometimes I will go back to a certain cell and change or add data to that cell and then move from there. In other words, I may not have to go through the entire sequence of cells and then back to the beginning.

    Here's the order of cells.

    D5, D7, D10, F10, D11, F11, D12, F12, D16, D17, D18, L16, L17, L18, D26

    Appreciate any help.

    Using Office 2016. Spreadsheet is protected. (added 8/20)
    Last edited by Wolfpacksr; 08-20-2019 at 01:21 PM.

  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: VBA to move cursor when ENTER is pressed

    Hello Wolfpacksr,

    Welcome to the Forum!

    If you have a workbook then please post it. This reduces time in answering your question and errors in code solutions.
    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
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA to move cursor when ENTER is pressed

    Right Click On Your Sheet Name At the Bottom Of Excel and Select View Code
    Paste this code in the module that opens and close it.

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA to move cursor when ENTER is pressed

    The usual approach is to use the worksheet Change event. Many have used the same or slightly modified version.

    In my example file, the first worksheet used mehmetcik's method.

    Worksheet 2 uses the traditional method with my added concept. That is, to move from a cell not changed, one must change it to fire the Change event. I used Copy/Paste to do that via Application.OnKey. I dealt with the issue of protected/locked cells a bit. It is a bit annoying that for a first Enter key press in a protected cell or one not in the TabOrder set, one needs to press Enterkey a 2nd time. To move, it restores the Enterkey and uses SendKeys() to send the key again. While I don't like it, it "sort of" works. The worksheet code relies on the module code as well.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: VBA to move cursor when ENTER is pressed

    mehmetcik, thanks for your reply. Unfortunately, it did not work for me. It works first time through, but once it goes back to the first cell (D5) it no longer works. Also, it doesn't allow me to click out of the sequence. So if I type something wrong in D12, I can't go back and correct it. Every time I click on D12, it continues running through the sequence (array).

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this ‼


    Next demonstration works as it is whatever a 32 bits or a 64 bits Excel 2010 and upper version.
    For previous versions just remove the PtrSafe statement …

    According to the initial post and guessing it's an unprotect worksheet (or the code should be easier !) a starter demonstration :

    PHP Code: 
    Private Declare PtrSafe Function GetKeyStateLib "User32.dll" (ByVal nVirtKey&)

    Private 
    Sub Worksheet_SelectionChange(ByVal Target As Range)
     Static 
    A$, SP$()
        
    Dim K%
            
    GetKeyState(13)
        If 
    "" Then
                SP 
    Split("D5,D7,D10,F10,D11,F11,D12,F12,D16,D17,D18,L16,L17,L18,D26"",")
            If 
    0 Then
                
    If IsNumeric(Application.Match(Target(0).Address(FalseFalse), SP0)) Then
                    A 
    Target(0).Address(FalseFalse)
                Else
                    
    0
                End 
    If
            
    End If
        
    End If
        If 
    0 Then
            
    If IsError(Application.Match(Target(0).Address(FalseFalse), SP0)) And _
               IsNumeric
    (Application.Match(Target.Address(FalseFalse), SP0)) Then
                A 
    Target.Address(FalseFalse)
            Else
                
    Application.Match(ASP0)
                
    SP(-(UBound(SP) + 1) * K)
                
    Application.EnableEvents False
                Range
    (A).Select
                Application
    .EnableEvents True
            End 
    If
        ElseIf 
    IsNumeric(Application.Match(Target.Address(FalseFalse), SP0)) Then
            A 
    Target.Address(FalseFalse)
        
    End If
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 08-20-2019 at 08:26 AM. Reason: optimization …

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb


    New version in previous post …

  8. #8
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: VBA to move cursor when ENTER is pressed

    Ken, thanks for the reply. Your program worked for the most part, but had some trouble copying it to my spreadsheet. It would look for your spreadsheet each time. I had to add my info to your spreadsheet. The code doesn't start until I select sheet 2 and then go back to sheet 1.

  9. #9
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Try this ‼

    Marc, thanks for the reply. I haven't had a chance to try it yet, but my spreadsheet is protected, will this change things?

    Just tried it. Doesn't work on protected spreadsheet. Sorry, I should have stated that previously.
    Last edited by Wolfpacksr; 08-20-2019 at 01:23 PM.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    As it well works too on my side on a protected sheet, no issue with a smart one …

    The reason why we asked for an attachment reflecting exactly the real one !
    As that needs some ajustments I won't even try to guess without any attachment
    but you can easily amend my code to fit your need with your worksheet …

  11. #11
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: VBA to move cursor when ENTER is pressed

    File attached.
    I had to remove some confidential info. Spreadsheet is protected. Sheet2 is for Lookup functions. Cells in red are for formulas and will be hidden. I've add/removed cells since the original post, so cell sequence has change.
    Cell Sequence = (D4, D6, D9, F9, D10, F10, D11, F11, D15, D16, D17, D18, D19, D20, D21, L15, L16, L17, D25, Q14, Q21).
    Attached Files Attached Files

  12. #12
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA to move cursor when ENTER is pressed

    Marc's macro works fine for both protected and unprotected worksheets and locked/unlocked cells. Since it is triggered by the worksheet Selection event, you must enable the Select Locked Cells option when you Protect that worksheet.

    Paste your new key sequence between his quotes for this line:
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

    My method is not meant for protected/locked cells in the taborder array as it is designed to Change the cell to trigger the macro. As such, one should protect the sheet with Select Locked Cells option unchecked.

    Marc's method can likely be modified to handle the Tab and Shift+Tab order as well.
    Last edited by Kenneth Hobson; 08-20-2019 at 03:44 PM.

  13. #13
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: VBA to move cursor when ENTER is pressed

    It now runs through the cell sequence, but I can't get out of the sequence, I have to run through the full sequence. If I'm in D16 and don't have any more information to enter I can't go to D4 and start over. It continues in the sequence where I left off. So, if I enter data in D4, when I press Enter the cursor jumps to D17.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this ‼


    According to your attachment as it is, 3 steps you must follow :

    • 3 : once both codes in their respective class modules, save the workbook as .xlsb binary format and close it.

    • 2 : paste this code to the ThisWorkbook module :

    PHP Code: 
    Private Sub Workbook_Open()
        
    Sheet1.Initialize
    End Sub 
    • 1 : paste next code to the Sheet1 worksheet module :

    PHP Code: 
    Private Declare PtrSafe Function GetKeyStateLib "User32.dll" (ByVal nVirtKey&)

    Dim A$, S$()

    Sub Initialize(Optional C$)
            
    Me.Activate
        
    If "" Then
            
    If Range(C).Locked False Then
                Application
    .EnableEvents False
                Range
    (C).Select
                Application
    .EnableEvents True
            End 
    If
        
    End If
            
    Selection(1).Address(FalseFalse)
            
    Split("D4,D6,D9,F9,D10,F10,D11,F11,D15,D16,D17,D18,D19,D20,D21,L15,L16,L17,D25,Q14,Q21"",")
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
            
    Dim P%
        If 
    GetKeyState(13) < 0 Then
                P 
    Application.Match(AS0)
                
    S(-(UBound(S) + 1) * P)
            If 
    Target(1).Address(FalseFalse) <> A Then
                Application
    .EnableEvents False
                Range
    (A).Select
                Application
    .EnableEvents True
            End 
    If
        Else
            
    Target(1).Address(FalseFalse)
        
    End If
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !

  15. #15
    Registered User
    Join Date
    08-14-2019
    Location
    Baltimore
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: VBA to move cursor when ENTER is pressed

    Thanks Marc, works great!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Request Macro to always move Cursor to right after ENTER or TAB key are pressed
    By Colton4 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-09-2015, 02:05 AM
  2. [SOLVED] Scanning move cursor on enter
    By paynod in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2015, 05:23 AM
  3. [SOLVED] To move cursor to the next row automatically when we enter press
    By catchnanan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-19-2013, 09:13 AM
  4. Hitting Enter to Move Cursor to the Next Entry Cell
    By Cisnerax in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-25-2006, 07:10 AM
  5. how do I move cursor without tab, enter or arrow in Excel
    By jlseagull in forum Excel General
    Replies: 1
    Last Post: 07-15-2005, 11:05 PM
  6. [SOLVED] Cursor not to move when pressing the enter key
    By Alex Martinez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2005, 03:06 PM
  7. Cursor not to move when hitting the enter key
    By Alex Martinez in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-12-2005, 01:06 AM

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