+ Reply to Thread
Results 1 to 11 of 11

Change behaviour of ENTER in selection with VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Change behaviour of ENTER in selection with VBA

    I have a Worksheet_SelectionChange event that selects the row to the left of and including the active cell, as well as the column above and including the active cell, with the active cell remaining the original cell.

    Problem: When I type something in the active cell and Enter, instead of moving one cell down (which is what is required) it jumps to the start of the selection, as per normal. How can I programmatically change this behaviour so that the cell immediately below the active cell (i.e. the corner of the selection) becomes the new active cell?

    Option Explicit
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim lngRows As Long, lngCols As Long
    Dim CornerCell As Range
    lngRows = Selection.Rows.Count
    lngCols = Selection.Columns.Count
    
    If lngRows > 1 Or lngCols > 1 Then Exit Sub
    
    Set CornerCell = ActiveCell
    Application.EnableEvents = False
    
    Union(Range(Cells(CornerCell.Row, 1), Cells(CornerCell.Row, CornerCell.Column)), _
    Range(Cells(1, CornerCell.Column), Cells(CornerCell.Row, CornerCell.Column))).Select
    CornerCell.Activate
    Application.EnableEvents = True
    End Sub
    Regards,
    Henk Stander
    Last edited by Henk Stander; 12-19-2014 at 07:47 AM.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Change behaviour of ENTER in selection with VBA

    Hi,

    Try to change the code to this :

    Option Explicit
    Dim CornerCell As Range
    Dim CrossRange As Range
    Private Sub Worksheet_Change(ByVal Target As Range)
      If CrossRange Is Nothing Then
         Application.EnableEvents = False
           Application.Undo
             Set CornerCell = ActiveCell
           Application.Undo
         Application.EnableEvents = True
         CornerCell.Offset(1).Activate
      ElseIf Selection.Address = CrossRange.Address Then
         CornerCell.Offset(1).Activate
      End If
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim lngRows As Long, lngCols As Long
    
      lngRows = Selection.Rows.Count
      lngCols = Selection.Columns.Count
      If lngRows > 1 Or lngCols > 1 Then Exit Sub
    
      Set CornerCell = ActiveCell
      Application.EnableEvents = False
        Set CrossRange = Union(Range(Cells(CornerCell.Row, 1), Cells(CornerCell.Row, CornerCell.Column)), _
                               Range(Cells(1, CornerCell.Column), Cells(CornerCell.Row, CornerCell.Column)))
        CrossRange.Select
        CornerCell.Activate
      Application.EnableEvents = True
    End Sub
    Regards
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Change behaviour of ENTER in selection with VBA

    @karedog:
    Thank you sir, it is working like a charm!
    I will study it some more.

    Regards,
    Henk

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Change behaviour of ENTER in selection with VBA

    You are welcome, glad I can help.

    Regards

  5. #5
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Change behaviour of ENTER in selection with VBA

    @karedog:
    I don't follow the logic of the following lines, first private sub:
    If CrossRange Is Nothing Then
         Application.EnableEvents = False
           Application.Undo
             Set CornerCell = ActiveCell
           Application.Undo
         Application.EnableEvents = True
         CornerCell.Offset(1).Activate
    The code works fine without it, what am I missing?

    Regards,
    Henk

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Change behaviour of ENTER in selection with VBA

    If you open this Excel file, and you don't move the cell selection (so the Worksheet_SelectionChange() is not fired), and change something in current cell, it will give an error.

    Regards

  7. #7
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Change behaviour of ENTER in selection with VBA

    Quote Originally Posted by karedog View Post
    If you open this Excel file, and you don't move the cell selection (so the Worksheet_SelectionChange() is not fired), and change something in current cell, it will give an error.

    Regards
    @karedog:
    I could not replicate your error. I have attached a file with the code as below. Note the lines that have been commented out:
    Option Explicit
    Dim CornerCell As Range
    Dim CrossRange As Range
    Private Sub Worksheet_Change(ByVal Target As Range)
      If CrossRange Is Nothing Then
         'Application.EnableEvents = False
           'Application.Undo
             'Set CornerCell = ActiveCell
          ' Application.Undo
         'Application.EnableEvents = True
         'CornerCell.Offset(1).Activate
      ElseIf Selection.Address = CrossRange.Address Then
         CornerCell.Offset(1).Activate
      End If
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim lngRows As Long, lngCols As Long
    
      lngRows = Selection.Rows.Count
      lngCols = Selection.Columns.Count
      If lngRows > 1 Or lngCols > 1 Then Exit Sub
    
      Set CornerCell = ActiveCell
      Application.EnableEvents = False
        Set CrossRange = Union(Range(Cells(CornerCell.Row, 1), Cells(CornerCell.Row, CornerCell.Column)), _
                               Range(Cells(1, CornerCell.Column), Cells(CornerCell.Row, CornerCell.Column)))
        CrossRange.Select
        CornerCell.Activate
      Application.EnableEvents = True
    End Sub
    Best Regards,
    Henk
    Attached Files Attached Files

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Change behaviour of ENTER in selection with VBA

    I mean if you first open your file, and without moving the pointer to another cell, type something, this is what will be happen :

    If the code is :
    Private Sub Worksheet_Change(ByVal Target As Range)
      If Selection.Address = CrossRange.Address Then
         CornerCell.Offset(1).Activate
      End If
    End Sub
    It will raise this error :

    Run-time error '91':
    Object variable or With block variable not set




    But if the code is :
    Private Sub Worksheet_Change(ByVal Target As Range)
      If CrossRange Is Nothing Then
      ElseIf Selection.Address = CrossRange.Address Then
         CornerCell.Offset(1).Activate
      End If
    End Sub
    Then no error will be raised, but then your problem of this case is happened again, press Enter to this cell will just move to another cell of current selection, not going down to below cell. If it is ok for you if the first active cell (and for first time only) this happened, then you can simply delete the code as you did in the sample file.

    Regards

  9. #9
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Change behaviour of ENTER in selection with VBA

    @karedog:
    Yes, I agree and it makes perfect sense. I must admit that I don't fully understand how the part that I commented out works, especially the application.undo lines. That it gets the job done is certain and I agree, your original code, with all the lines is the most efficient in all scenarios.

    Would you care to explain that first part please?

    Best Regards,
    Henk

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Change behaviour of ENTER in selection with VBA

    Of course, here it is :

    Application.EnableEvents = False
      Application.Undo
        Set CornerCell = ActiveCell
      Application.Undo
    Application.EnableEvents = True
    As we know, Excel doesn't have event like Worksheet_BeforeChange(), we can only detect changes after some changes is made. So we must use tricks to do this.

    Say we are examining cell A1, and the current value of this cell is 1.
    Now lets say we type 2 in this cell. How can we get the previous value of this cell ?
    Worksheet_Change() event only fired after the cell is changed, and we don't have something like Worksheet_BeforeChange() event.

    So we undo the changes, now we got the previous value of this cell (that is 1), we save this value to a variable. Now we must revert back the cell to the last state (the cell value is 2). If we do this in the worksheet page, we hit the Redo button, but for VBA, strange enough, we must again called undo. So the second undo is actually a Redo action.

    For this case, we want to know the location of the corner cell before the changes is made, so we undo, assign the range to a variable, then undo again (which actually redo) to restore the last state.

    Regards

  11. #11
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Change behaviour of ENTER in selection with VBA

    @karedog:
    Thank you karedog, that explains everything - much appreciated.

    Have a blessed season!

    Regards,
    Henk

+ 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. 'Tab' 'Enter' does not change cell selection
    By kheli in forum Excel General
    Replies: 0
    Last Post: 08-31-2011, 04:22 PM
  2. Centre across selection - strange behaviour when wrapping.
    By talksalot81 in forum Excel General
    Replies: 0
    Last Post: 04-27-2010, 12:34 PM
  3. How to change the behaviour of the "Enter" key
    By calvinbaisley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2009, 07:24 PM
  4. [SOLVED] [SOLVED] simulate a enter key behaviour?
    By Liedson31 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2005, 10:05 AM
  5. how to change (override) enter key behaviour in a cell
    By helpwithXL in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-01-2005, 02:06 PM

Tags for this Thread

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