Sort Based on 2 conditions

MM91

Board Regular
Joined
Nov 29, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
So in column A I have a numerical value that i need to sort by number on column a and then by type on column L. I am piecing together a code using the macro recorder but I able to sort them seperately but it messes up the sort order once I try to secondary sort by column L. Any help or guidance would be appreciated!




EXCEL HELP.png


VBA Code:
Option Explicit
Sub SortMyData()
Dim lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("A7:Q" & lr).Sort key1:=Range("A7"), order1:=1
Application.ScreenUpdating = True

    Cells.Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("L3:L25") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A3:L25")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

    End With

End Sub
 
Sorry but that makes no sense, in your image there are no dates.
Apologies! I added the dates to the column b to show what I want to accomplish. I need to first sort by section # in column A, then by part type (Purchased or Prod. Order) and then by date so the last added part will be at the bottom of that section. Hopefully that makes sense! thank you!!
 

Attachments

  • MACRO.png
    MACRO.png
    63 KB · Views: 5
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Ok, how about
VBA Code:
Sub SortMyData()
   Dim lr As Long
   Application.ScreenUpdating = False
   lr = Cells(Rows.Count, 1).End(xlUp).Row
   Range("A7:Q" & lr).Sort Range("A7"), xlAscending, Range("L7"), , xlAscending, Range("B7"), xlAscending, xlNo
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub SortMyData()
   Dim lr As Long
   Application.ScreenUpdating = False
   lr = Cells(Rows.Count, 1).End(xlUp).Row
   Range("A7:Q" & lr).Sort Range("A7"), xlAscending, Range("L7"), , xlAscending, Range("B7"), xlAscending, xlNo
End Sub
thank you!! the only change I have would be it puts produced orders before purchased orders. Is it as simple as changing ascending for sort range ("L7") to descending? your help is much appreciated!!
 
Upvote 0
thank you!! the only change I have would be it puts produced orders before purchased orders. Is it as simple as changing ascending for sort range ("L7") to descending? your help is much appreciated!!
answered my own question, it worked great! thanks!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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