Sorting by multiple columns

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have data that needs sorting by date first and then request number within each date range.

This is my code
VBA Code:
Sub Sorting()
'This code sorts by the date first and within each date, it sorts the rows by the request number
With ActiveSheet.Sort
     .SortFields.Add Key:=Range("A3"), order:=xlAscending
     .SortFields.Add Key:=Range("C3"), order:=xlAscending
     .SetRange Range("A3", Range("O" & Rows.Count).End(xlDown))
     .Header = xlYes
     .Apply
End With
End Sub

  • The header row is row 3
  • Date is column A
  • Request number is column C
What is wrong with my code as it sorts by date but will not sort within that by request number?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Is there data in ALL columns ?
AND
What does the request number look like ?
 
Upvote 0
The request numbers looks like 10250 and there should be data in most columns.
 
Upvote 0
By the way, I get a sort warning when I try and run this with both options creating an infinite loop.
 
Upvote 0
You'll get an error if you try to sort with a blank column somewhere in the range !!
 
Upvote 0
This code works for me Michael for sorting by date.
VBA Code:
Sub SortCells()

Range("A4", Range("O" & Rows.Count).End(xlDown).Address).Sort Key1:=Range("A4"), Order1:=xlAscending
End Sub

How do I change it to sort by the request number within each date?
 
Upvote 0
Does this work for you...UNTESTED

VBA Code:
Sub MM1()
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
With ActiveSheet
.Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=Range("A3:A" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("C3:C" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A3:O" & lr)
        .Header = xlYes
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With
End Sub
 
Upvote 0
There may be things entered under the data so I can't use the xlup method and rows.count to find the number of rows. There should not be any gaps in the date so I was thinking the xldown method on column A. I am just a little unsure on the syntax to change around the third line of code.
 
Upvote 0
Thanks Michael,

That sorts everything by date (column A) but it only sorts the first instance of the date by request number. By that I mean that for the first date going down the list, the request number is sorted but for every additional group of dates in the list, the request numbers are not sorted.
 
Upvote 0
Actually, don't worry Michael, I don't need it anymore. But thanks anyway.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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