Sort By Column D THEN auto-scroll to currently selected Row's Column D value?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
Office Version
  1. 365
Platform
  1. Windows
I have code that sorts from A-Z based on Column D's values. After the Sort completes the cursor parks itself at B4. Works fine, but instead of parking at B4 I'd like to have the cursor scroll to the Value that was in Column D for the Row that was selected when the Sort macro was run.

Here's my current code...

Code:
Sub SortBySub()
Application.ScreenUpdating = False
    ActiveWindow.SmallScroll Down:=-189
    Rows("5:2000").Select
    ActiveWindow.ScrollRow = 1426
    ActiveWindow.ScrollRow = 4
    ActiveWorkbook.Worksheets("Calendar").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Calendar").Sort.SortFields.Add Key:=Range( _
        "D5:D2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Calendar").Sort.SortFields.Add Key:=Range( _
        "C5:C2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Calendar").Sort.SortFields.Add Key:=Range( _
        "B5:B2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Calendar").Sort
        .SetRange Range("A5:BQ2000")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B4").Select
    Application.ScreenUpdating = True
End Sub

Make sense?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try:
Code:
Sub SortBySub()

    Dim LR          As Long
    Dim rng         As Range
    Dim rngStart    As Range
    
    LR = Cells(Rows.count, 4).End(xlUp).row
    Set rng = ActiveWorkbook.Worksheets("Calendar").Range("A5:BQ" & LR)
    Set rngStart = ActiveCell
    
    Application.ScreenUpdating = False
    
    rng.Sort.SortFields.Clear
    rng.Sort.SortFields.add key:=Range("D5:D" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
    rng.Sort.SortFields.add key:=Range("C5:C" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
    rng.Sort.SortFields.add key:=Range("B5:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
    With rng.Sort
        .SetRange Range("A5:BQ" & LR)
        .header = xlGuess
        .MatchCase = False
        .Orientation = XlTopBottom
        .SortMethod = xlPinYin
        .Apply
    End With
        
    With Application
        .Goto rngStart, True
        .ScreenUpdating = True
    End With
    
    Set rng = Nothing
    Set rngStart = Nothing
    
End Sub
 
Last edited:
Upvote 0
Try:
Code:
Sub SortBySub()

    Dim LR          As Long
    Dim rng         As Range
    Dim rngStart    As Range
    
    LR = Cells(Rows.count, 4).End(xlUp).row
    Set rng = ActiveWorkbook.Worksheets("Calendar").Range("A5:BQ" & LR)
    Set rngStart = ActiveCell
    
    Application.ScreenUpdating = False
    
    rng.Sort.SortFields.Clear
    rng.Sort.SortFields.add key:=Range("D5:D" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
    rng.Sort.SortFields.add key:=Range("C5:C" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
    rng.Sort.SortFields.add key:=Range("B5:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
    With rng.Sort
        .SetRange Range("A5:BQ" & LR)
        .header = xlGuess
        .MatchCase = False
        .Orientation = XlTopBottom
        .SortMethod = xlPinYin
        .Apply
    End With
        
    With Application
        .Goto rngStart, True
        .ScreenUpdating = True
    End With
    
    Set rng = Nothing
    Set rngStart = Nothing
    
End Sub

Should
Code:
.Orientation = XlTopBottom
be
Code:
.Orientation = XlTopToBottom
?
 
Last edited:
Upvote 0
Yes, typo in my code

I'm getting this error:

Run-time error '1004':

Unable to get the Sort property of the Range class


When I debug this is highlighted:

rng.Sort.SortFields.Clear
 
Last edited:
Upvote 0
Try:
Code:
Sub SortBySub()

    Dim LR          As Long
    Dim rng         As Range
    Dim wks         As Worksheet
    
    LR = Cells(Rows.count, 4).End(xlUp).row
    Set wks = ActiveWorkbook.Worksheets("Calendar")
    Set rng = ActiveCell

    Application.ScreenUpdating = False
        
    With wks
        .Sort.SortFields.Clear
        .Sort.SortFields.add key:=Range("D5:D" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
        .Sort.SortFields.add key:=Range("C5:C" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
        .Sort.SortFields.add key:=Range("B5:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
        With .Sort
            .SetRange wks.Range("A5:BQ" & LR)
            .header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
    
    With Application
        .Goto rng, True
        .ScreenUpdating = True
    End With
    
    Set rng = Nothing
    
End Sub
 
Last edited:
Upvote 0
Sorry for the delay!

This is close but the cursor doesn't follow the originally selected row (and most importantly the data it contains), it parks at the original row NUMBER. After sorting the originally selected row has become another row number. I want this macro to follow that original row no matter which number it becomes after the sort.

Make sense?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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