Link to cross-post:
https://www.excelforum.com/excel-pr...3643-vba-to-2-step-sort-multiple-columns.html
https://www.excelforum.com/excel-pr...3643-vba-to-2-step-sort-multiple-columns.html
I have an Excel spreadsheet that I need to sort 3 columns and varying rows. What I would like is VBA code to sort all columns and rows through item 'P' by Quantity (largest to smallest). Then I need to sort all 3 columns a second time by Sort Order (smallest to largest) neglecting the items with zero quantities. The number of items will always be the same so the first sort range will always be the same.
Here is a link to the same in Google Sheets (for reference/example only): https://goo.gl/oj44Ec
I tried to record a macro for this but it only works for this specific example (Items may not always have quantities and/or the quantities will change value so the second sort range (rows) will change). Here is what the macro recorded:
Can someone provide help? Thanks!
https://www.excelforum.com/excel-pr...3643-vba-to-2-step-sort-multiple-columns.html
https://www.excelforum.com/excel-pr...3643-vba-to-2-step-sort-multiple-columns.html
I have an Excel spreadsheet that I need to sort 3 columns and varying rows. What I would like is VBA code to sort all columns and rows through item 'P' by Quantity (largest to smallest). Then I need to sort all 3 columns a second time by Sort Order (smallest to largest) neglecting the items with zero quantities. The number of items will always be the same so the first sort range will always be the same.
Here is a link to the same in Google Sheets (for reference/example only): https://goo.gl/oj44Ec
I tried to record a macro for this but it only works for this specific example (Items may not always have quantities and/or the quantities will change value so the second sort range (rows) will change). Here is what the macro recorded:
Code:
Sub Sort1()
'
' Sort1 Macro
'
'
Range("A4:C20").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A5:A20") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A4:C20")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A4:C16").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C5:C16") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A4:C16")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Can someone provide help? Thanks!
Last edited: