Best way to sort a table by month & day ignoring year

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I have a table of family members with their dates of birth. I would like to sort it by month and day ignoring the year. Here is a sample table. The only way I could think of was to add a helper column with just the month and day. Is there a better way that does not require the helper column?

Christmas & Birthday Candy.xlsx
CDE
4NameDoBm/dd
5Joe1/21/198001.21
6Sally2/03/194402.03
7Billy5/24/197805.24
8Nancy8/15/195508.15
9Henry10/10/199410.10
10Linda12/17/201212.17
Sheet1
Cell Formulas
RangeFormula
E5:E10E5=TEXT([@DoB],"mm.dd")
 
An alternative with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DoB", type date}}),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Changed Type", "Text Before Delimiter", each Text.BeforeDelimiter(Text.From([DoB], "en-US"), "/", {0, RelativePosition.FromEnd}), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Text Before Delimiter",{"DoB"})
in
    #"Removed Columns"
I have been meaning to get up to speed with Power Query. This will give me another incentive. Thanks
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Thanks to everyone for the suggestions. I am going to want to sort the table dynamically on different columns, so I think I am going to stick with my original solution. That way I can right-click a column and select sort.
 
Upvote 0
Just for fun Jennifer, I thought I'd look at a VBA option. Here it is to play with:

VBA Code:
Option Explicit
Sub JenMurphy()
    Dim a, tmp, i As Long, j As Long
    a = ActiveSheet.ListObjects("Table1").DataBodyRange     '<-- *** Just check the Table name ***
    ReDim Preserve a(1 To UBound(a, 1), 1 To 4)
    Dim r As Range, c As Range
    Set r = Range("D5", Cells(Rows.Count, "D").End(xlUp))
    
    i = 1
    For Each c In r
        a(i, 4) = Evaluate(CLng(c) & "-DATE(YEAR(" & CLng(c) & "),1,1)+1")
        i = i + 1
    Next c
    For i = 1 To UBound(a, 1)
        For j = i + 1 To UBound(a, 1)
            If a(i, 4) > a(j, 4) Then
                tmp = a(j, 1): a(j, 1) = a(i, 1): a(i, 1) = tmp
                tmp = a(j, 2): a(j, 2) = a(i, 2): a(i, 2) = tmp
                tmp = a(j, 3): a(j, 3) = a(i, 3): a(i, 3) = tmp
                tmp = a(j, 4): a(j, 4) = a(i, 4): a(i, 4) = tmp
            End If
        Next j
    Next i
    Range("C5").Resize(UBound(a, 1), 3) = a
End Sub

Before:
Book1
CDE
4NameDoBm/dd
5Sally2/03/194402.03
6Nancy8/15/195508.15
7Billy5/24/197805.24
8Joe1/21/198001.21
9Henry10/10/199410.10
10Linda12/17/201212.17
Sheet1
Cell Formulas
RangeFormula
E5:E10E5=TEXT([@DoB],"mm.dd")


After:
Book1
CDE
4NameDoBm/dd
5Joe1/21/19801.21
6Sally2/03/19442.03
7Billy5/24/19785.24
8Nancy8/15/19558.15
9Henry10/10/199410.1
10Linda12/17/201212.17
Sheet1
 
Upvote 0
... Is there a better way that does not require the helper column?..
Just for fun: Yes, there is (not sure about "better" as it might take hours to set up) -- conditional formatting that assigns a specific fill color for each month and a specific font color for each date. Sorting by fill color then by font color. No helper column.
 
Upvote 0
Just for fun: Yes, there is (not sure about "better" as it might take hours to set up) -- conditional formatting that assigns a specific fill color for each month and a specific font color for each date. Sorting by fill color then by font color. No helper column.
This forum is an absolute gold mine of creative and diverse ways to solve any problem.
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,035
Members
449,092
Latest member
ikke

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