Sort by earliest, sort by alphabetical when equal

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm using the following code to sort times so the earliest appears first

VBA Code:
    Dim ws As Worksheet
    Dim rng As Range
    Dim srtcol As Range

    Set ws = Worksheets(2)
    Set rng = ws.Range("A1:Q1", ws.Range("Q1").End(xlDown))
    Set srtcol = ws.Range("K1", ws.Range("K1").End(xlDown))
    'sorts by earliest start time in K
    rng.Sort Key1:=srtcol, Order1:=xlAscending

Probably a simpler way of doing that, but it does exactly what I want it to do (that part at least). I'm trying to add functionality to it, so after sorting by earliest time (in K) in then sorts alphabetically (which would be C) but so it doesn't overwrite the chronological order.

Basically, if the times in K are the same, a secondary sort between those identical times is performed where they appear in alphabetical order.
For context, I'm dealing with a list of names (last names) in C and shift start times in K. So there will be multiple instances of the same time occuring e.g 5 names at 9am, 10 names at 1pm..
Hopefully I'm making sense with my explanation, I've not had a coffee yet so I'm still a bit delirious.

Thanks all
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
VBA Code:
    rng.Sort ws.Range("K1"), xlAscending, ws.Range("C1"), , xlAscending, Header:=xlYes
This assumes you have a header row in row 1
 
Upvote 0
Solution
How about
VBA Code:
    rng.Sort ws.Range("K1"), xlAscending, ws.Range("C1"), , xlAscending, Header:=xlYes
This assumes you have a header row in row 1
much simpler than I thought it would be, thank you
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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