Sort Date by last date in clumn A

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Hi anyone,

I'm trying to figure out a way how to make the following code to sort date in column A of the active sheet by the last date on bottom row and the first date on the top row. Meaning in an ascending manner.

Code:
Sub SortDate()
    Dim c As Integer
    With ActiveSheet.Range("A1").CurrentRegion
        c = .Find(What:="Date", After:=.Cells(1, 1), LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False).Column
            .Sort Key1:=.Cells(1, c), Order1:=xlDescending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End With
End Sub
My column headers are on row 4. And I have data columns from column A to column I.

At present the code sorts date by the last date on top and first date on bottom. Meaning in an descending manner.

Any help on this would be kindly appreciated.

Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this

Code:
Sub SortDate()
    Dim c As Integer
    With ActiveSheet.Range("A1").CurrentRegion
        c = .Find(What:="Date", After:=.Cells(1, 1), LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False).Column
            .Sort Key1:=.Cells(1, c), Order1:=xl[COLOR=red]A[/COLOR][COLOR=red]scending[/COLOR], Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End With
End Sub
 
Upvote 0
Thanks for the help. If my column headers are in row 4 how should I change the code accordingly. Any help would be kindly appreciated.
 
Upvote 0
is the data always in the same range i.e A4:I31 or is this like likey to change. if so will it always have the same amount of rows but variable columns?
 
Upvote 0
The data range always get changed. Meaning it always does get added from 5 and onwards. But the data always stays between column A and I.

If so how should I change the code accordingly. Any help on this would be kindly appreciated.
 
Upvote 0
Sorry for the late reply, this is untested but should work

Code:
Sub sortbydate()
    Range("A4").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Sort Key1:=Range("A4"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
End Sub

Not the 'proper' way to do it but with out having a play best I can come up with
 
Upvote 0
Sorry for being late to response. Any ways thanks for the help. I do really appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

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