Aditya Jhala
Aditya Jhala

Reputation: 11

VBA Sort not working

I made a VBA code for a macro used for an excel file for some calculation. It requires sorting an excel sheet by two columns one by one. It is sorting the first column but is not able to sort the other although the code for both is exactly same except for the column number. Here is the code snippet for that sheet's calculation:

Sheets("Restock Clusters").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
LastRow = Range("B:C").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow)
Range("D3:F3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("D2:F2").Select
Selection.AutoFill Destination:=Range("D2:F" & LastRow)
Range("C1").Select
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Restock Clusters").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Restock Clusters").AutoFilter.Sort.SortFields.Add _
    Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlDescending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Restock Clusters").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Range("A1").Select
Application.CutCopyMode = False
Selection.AutoFilter
Selection.AutoFilter
ActiveWorkbook.Worksheets("Restock Clusters").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Restock Clusters").AutoFilter.Sort.SortFields.Add _
    Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Restock Clusters").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ActiveSheet.Calculate

Upvotes: 1

Views: 1111

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149295

Please Avoid the use of .Select It usually slows down your code besides sometimes causing runtime errors.

Is this what you are trying (Untested)?

Sub Sample()
    Dim ws As Worksheet
    Dim Lastrow As Long

    Set ws = ThisWorkbook.Sheets("Restock Clusters")

    With ws
        '~~> Where is the copy code???

        .Range("B1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        Lastrow = .Range("B:C").Find("*", searchorder:=xlByRows, _
                  searchdirection:=xlPrevious).Row

        .Range("A3:A" & Lastrow).ClearContents

        .Range("A2:A" & Lastrow).Formula = .Range("A2").Formula

        .Range("D3:F" & Lastrow).ClearContents

        .Range("D2:F" & Lastrow).Formula = .Range("D2:F2").Formula

        '~~> Sort Col C
        .Columns(3).Sort Key1:=.Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

        '~~> Sort Col A
        .Columns(1).Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With
End Sub

Upvotes: 1

Related Questions