2

I am trying to sort data in a spreadsheet by three different values:

  1. Column F - Values - Oldest to Newest
  2. Column B - Values - Smallest to Largest
  3. Column A - Values - Z to A

The code stops with an error on the .Apply section. I fear the cell selection is the issue.

Sub DatePartOrderSort()
'
' DatePartOrderSort Macro
'
'
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    Range(Cells(5, "A"), Cells(LR, "J")).Select
    '
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
    Cells(5, "A"), Cells(LR, "J")), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
    Cells(5, "A"), Cells(LR, "J")), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
    Cells(5, "A"), Cells(LR, "J")), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Date Order").Sort
        .SetRange Range(Cells(5, "A"), Cells(LR, "J"))
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

1 Answer 1

2

Try this:

Sub DatePartOrderSort()
'
' DatePartOrderSort Macro
'
'
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    Range(Cells(5, "A"), Cells(LR, "J")).Select
    '
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
    "F5:F" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
    "B5:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Date Order").Sort.SortFields.Add Key:=Range( _
    "A5:A" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Date Order").Sort
        .SetRange Range(Cells(5, "A"), Cells(LR, "J"))
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you for this - I now have lots of blank rows and I want to leave the last two blank is there anyway of using VBA to do this.
put this into a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.