Sort date column by year Excel vba

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
Folks: How can I sort a date column by year without creating a parallel, offset column that holds the formula like Year(rcell.value)? like:
Code:
'Sort
    'change sort keys to year(column 3.value), column 1, column 3
    sheet1.UsedRange.Offset(6, 0).Sort _
    key1:=Sheet1.UsedRange.>>Year of these values<<.Columns(1).Offset(7, 0).Resize(1, 1), _
    order1:=xlAscending, _
    key2:=Sheet1.UsedRange.Columns(1).Offset(7, 0).Resize(1, 1), _
    order1:=xlAscending, _
    key3:=Sheet1.UsedRange.Columns(3).Offset(7, 0).Resize(1, 1), _
    order1:=xlAscending, _
    Header:= _
        xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
My guess is impossible, but...
Thank you - Rowland
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Is the value a real date?
If so just sort by that. Anything sorted by data will be sorted by year inherently (as year is part of date). It is impossible to sort by date and not have the years come out right.
 
Upvote 0
That's what I realized when I sorted it, since Excel uses the date serial so it recognizes proper date order.

However, I think my End User may actually wants sort by year only descending, then by product name ascending, then by full date descending. That, I haven't figured out yet, but doesn't seem possible without using an additional column for year only like formula = year(date).

Thank you, Rowland
 
Upvote 0
However, I think my End User may actually wants sort by year only descending, then by product name ascending, then by full date descending. That, I haven't figured out yet, but doesn't seem possible without using an additional column for year only like formula = year(date).
I believe you are correct. It needs a column to sort by. I don' think it is possible without one. However, if you do this with a macro, you can have the macro insert a temporary column that gets the year, sorts the records like you want, then delete the temporary column.
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,050
Members
452,542
Latest member
Bricklin

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