VBA to sort 4 criteria in order

vavs

Well-known Member
Joined
Jul 2, 2004
Messages
514
I have 6 columns in a spreadsheet. I need to sort the spreadsheet based on Column B then Column C then Column D and finally Column A. Columns B and C contain array formulas. Column D contains either Yes or No. I was planning on sorting this column in descending order. Finally Column A is both text and numbers. I would like to sort this as anything that looks like a number is a number.

Here is what I put in place. The only problem is that I am getting inconsistent results. I have approximately 10K rows of data.

Code:
Sub Cleanup()
    Application.StatusBar = "Filter and sort and you are done."
    Worksheets("USCopy1").Select
    Columns("A:F").Select
    
    
    
    ActiveWorkbook.Worksheets("USCopy1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("USCopy1").Sort.SortFields.Add Key:=Range( _
        "B:B"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("USCopy1").Sort.SortFields.Add Key:=Range( _
        "D:D"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("USCopy1").Sort.SortFields.Add Key:=Range( _
        "C:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("USCopy1").Sort.SortFields.Add Key:=Range( _
        "A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("USCopy1").Sort
        .SetRange Range("A:F")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Columns("A:F").Select
    ActiveSheet.Range("A:F").AutoFilter
    
End Sub

So what I eventually need to do is Sort the column B so that the largest number is in row 1, then any duplicate values from B sort column C in status of Yes before No, any duplicate Yes or No, sort by the oldest date in Column D, finally alpha numeric sort of column A after all criteria is met.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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