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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What is the first row with data? part of your code is starting in row 3 & part in row 7
 
Upvote 0
All of my data that needs to be sorted starts in cell A7 to column Q on down.
 
Upvote 0
Is row 7 a header row, or data?
 
Upvote 0
row 7 is data, rows 1-6 are header rows. Thank you!
 
Upvote 0
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, Header:=xlNo
End Sub
 
Upvote 0
that worked perfect thank you so much! Much simpler than what I was trying to do
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Hi I need to add an additional situation for sorting where after it sorts by the date. How would I add that to this code?
Thank you!
 
Upvote 0
Sorry but that makes no sense, in your image there are no dates.
 
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,390
Members
449,222
Latest member
taner zz

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