Select Cell Data Last Entered Into After Auto-Sort

LBinGA

Board Regular
Joined
Jan 29, 2014
Messages
57
Hello all!

I have the following code that auto-sorts my table based on a value in column F. Once sorted, the cursor goes to the bottom of the table. How would I change this code to force the cursor to "follow" the cell in which data was last entered after the sort? The cell is dynamic because of the sort, and not static.
Code:
Private Sub Worksheet_Change(ByVal Target As Range) 
Dim finalRow As Integer
Dim i As Integer
Dim erow As Long

erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Range("f:f").Select
Range("b2").Sort Key1:=Range("f2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
If Cells(erow - 1, 1).Offset(0, 1) = "" Then
Cells(erow - 1, 1).Offset(0, 1).Select
Else
Cells(erow, 1).Select
End If
 
    finalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("A1:A" & finalRow).EntireRow.AutoFit
    
    For i = 2 To finalRow
        If Range("A" & i).EntireRow.RowHeight < 27 Then
            Range("A" & i).EntireRow.RowHeight = 27
        If Range("D" & i).EntireRow.RowHeight < 27 Then
          Range("D" & i).EntireRow.RowHeight = 27
        End If
        End If
            Next i
    
    On Error Resume Next
 
End Sub

Thanks in advance,

LBinGA
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Code:
Private Sub Worksheet_Change(ByVal Target As Range) 
Dim finalRow As Integer
Dim i As Integer
Dim erow As Long

[COLOR="#FF0000"]Dim tCell as Range, tValue as Variant
Set tCell = Target.Cells(1,1)
Set tValue = tCell.Value[/COLOR]

erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Range("f:f").Select
Range("b2").Sort Key1:=Range("f2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

[COLOR="#FF0000"]'If Cells(erow - 1, 1).Offset(0, 1) = "" Then
'Cells(erow - 1, 1).Offset(0, 1).Select
'Else
'Cells(erow, 1).Select
'End If[/COLOR]
 
    finalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("A1:A" & finalRow).EntireRow.AutoFit
    
    For i = 2 To finalRow
        If Range("A" & i).EntireRow.RowHeight < 27 Then
            Range("A" & i).EntireRow.RowHeight = 27
        If Range("D" & i).EntireRow.RowHeight < 27 Then
          Range("D" & i).EntireRow.RowHeight = 27
        End If
        End If
            Next i
    
    On Error Resume Next
  [COLOR="#FF0000"]  tCell.EntireColumn.Find(what:=tValue, LookAt:= xlWhole).Select[/COLOR]
End Sub
 
Last edited:
Upvote 0
Very interesting! Thank you. It works for the most part. I was getting a Runtime Error 13: Type Mismatch, so I took "Set" out of the tvalue, as bolded below:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)Dim finalRow As Integer
Dim i As Integer
Dim erow As Long
Dim tCell As Range, tValue As Variant


Set tCell = Target.Cells(1, 1)
[B]tValue = tCell.Value[/B]


erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Range("f:f").Select
Range("b2").Sort Key1:=Range("f2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal


If Cells(erow - 1, 1).Offset(0, 1) = "" Then
Cells(erow - 1, 1).Offset(0, 1).Select
Else
Cells(erow, 1).Select
End If
 
    finalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("A1:A" & finalRow).EntireRow.AutoFit
    
    For i = 2 To finalRow
        If Range("A" & i).EntireRow.RowHeight < 27 Then
            Range("A" & i).EntireRow.RowHeight = 27
        If Range("D" & i).EntireRow.RowHeight < 27 Then
          Range("D" & i).EntireRow.RowHeight = 27
        End If
        End If
            Next i
    
    On Error Resume Next
    tCell.EntireColumn.Find(what:=tValue, LookAt:=xlWhole).Select
End Sub

In doing so, the cursor goes to the first number/score that matches the number entered in the column. There may be duplicate scores in the column so this creates a problem.

Example: If the last item in the list is scored 39, the sheet re-sorts the rows and the cursor lands on the first score of 39 it 'sees' in the column, which may or may not be the entry just made.

Is there an adjustment for the cursor staying with the entry made?

Thanks,

LBinGA
 
Upvote 0
Anyone have a possible solution for keeping the cursor with the last cell entered on auto-sort? The code above, while great, doesn't take into account duplicates in the column.

Thanks,
LBinGA
 
Upvote 0
Change these lines at the beginning
Code:
Set tCell = Target.EntireRow.Cells(1,1)
Set tValue = tCell.Value

and this at the end
Code:
tCell.EntireColumn.Find(what:=tValue, LookAt:= xlWhole).Cells(1, 7).Select
 
Upvote 0
Perfect! I changed the bottom code to (1,1) and the cursor stays with the column entered. Beautifully done...thank you so much!

LBinGA
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,007
Members
449,203
Latest member
Daymo66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top