How to sort data by date format

%3CLINGO-SUB%20id%3D%22lingo-sub-2763918%22%20slang%3D%22en-US%22%3EHow%20to%20sort%20data%20by%20date%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2763918%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20-%20I'm%20using%20PC%2FWindows%2010%2C%20and%20Excel%20version%202018%3C%2FP%3E%3CP%3EMy%20excel%20data%20looks%20like%20this.%20I%20have%201500%20rows.%26nbsp%3B%3C%2FP%3E%3CTABLE%20width%3D%22751%22%3E%3CTBODY%3E%3CTR%3E%3CTD%20width%3D%22175%22%3EDate%20of%20hire%3C%2FTD%3E%3CTD%20width%3D%2272%22%3EGender%3C%2FTD%3E%3CTD%20width%3D%2272%22%3ERace%2FEthnicity%26nbsp%3B%3C%2FTD%3E%3CTD%20width%3D%2272%22%3ELevel%3C%2FTD%3E%3CTD%20width%3D%2272%22%3EPromotion%20Date%3C%2FTD%3E%3CTD%20width%3D%2272%22%3Enew%20level%3C%2FTD%3E%3CTD%20width%3D%2272%22%3Eterm%20date%3C%2FTD%3E%3CTD%20width%3D%2272%22%3Etype%20of%20term%3C%2FTD%3E%3CTD%20width%3D%2272%22%3ERegion%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E02%2F24%2F2003%3C%2FTD%3E%3CTD%3EFemale%3C%2FTD%3E%3CTD%3EWhite%3C%2FTD%3E%3CTD%3E1%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3EInvoluntary%3C%2FTD%3E%3CTD%3EEurope%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E05%2F09%2F2005%3C%2FTD%3E%3CTD%3EMale%3C%2FTD%3E%3CTD%3EAfrican%20American%3C%2FTD%3E%3CTD%3E2%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3EVoluntary%3C%2FTD%3E%3CTD%3EAsia%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E10%2F15%2F2007%3C%2FTD%3E%3CTD%3EMale%3C%2FTD%3E%3CTD%3EHispanic%3C%2FTD%3E%3CTD%3E3%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3En%2Fa%3C%2FTD%3E%3CTD%3ENorth%20America%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3EThe%20problem%20is%20the%20date%20data%20is%20in%20different%20formats.%20Some%20are%20formatted%20as%20general%20m%2Fd%2Fyy%2C%20but%20some%20are%20formatted%20as%20custom%3A%20d%2Fm%2Fyy.%20If%20I%20change%20all%20the%20dates%20into%20one%20format%20I%20end%20up%20changing%20the%20months%20to%20days%20or%20vice%20versa.%26nbsp%3B%20I'd%20like%20to%20sort%20the%20table%20by%20the%20date%20format%20so%20I%20can%20just%20change%20one%20half%20of%20the%20dates%20to%20the%20other%20half's%20format.%20Any%20ideas%3F%20Thanks%2C%20Emma%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2763918%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2763956%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sort%20data%20by%20date%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2763956%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1159990%22%20target%3D%22_blank%22%3E%40Emma_Sabin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPress%20Alt%2BF11%20to%20activate%20the%20Visual%20Basic%20Editor.%3C%2FP%3E%0A%3CP%3ESelect%20Insert%20%26gt%3B%20Module.%3C%2FP%3E%0A%3CP%3ECopy%20the%20following%20custom%20function%20into%20the%20code%20window%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3EFunction%20NumberFormat(rng%20As%20Range)%0A%20%20%20%20NumberFormat%20%3D%20rng.NumberFormat%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EClose%20the%20Visual%20Basic%20Editor.%3C%2FP%3E%0A%3CP%3EIn%20the%20first%20column%20next%20to%20the%20data%2C%20enter%20the%20following%20formula%20in%20row%202%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DNumberFormat(A2)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EFill%20down%20to%20the%20last%20row%20with%20data%2C%20for%20example%20by%20double-clicking%20the%20fill%20handle%20in%20the%20lower%20right%20of%20the%20cell%20with%20the%20formula.%3C%2FP%3E%0A%3CP%3ENow%20sort%20the%20entire%20range%20(not%20just%20the%20new%20column!)%20on%20the%20new%20column.%3C%2FP%3E%0A%3CP%3ECells%20with%20the%20same%20number%20format%20will%20be%20grouped%20together.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2764918%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sort%20data%20by%20date%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2764918%22%20slang%3D%22en-US%22%3Ehow%20can%20you%20tell%20which%20date%20is%20formatted%20which%20if%20the%20date%20is%2012%20or%20less%20and%20the%20month%20is%2012%20or%20less%3F%20i.e%2012%2F12%2F2007%3F%20or%209%2F12%2F2007%3F%20or%2012%2F9%2F2007%3F%20How%20can%20you%20tell%20which%20number%20is%20the%20day%20and%20which%20number%20is%20the%20month%20and%20how%20do%20you%20decide%20how%20to%20sort%20them%3F%20by%20month%3F%20or%20by%20day%3F%3C%2FLINGO-BODY%3E
New Contributor

Hi - I'm using PC/Windows 10, and Excel version 2018

My excel data looks like this. I have 1500 rows. 

Date of hireGenderRace/Ethnicity LevelPromotion Datenew levelterm datetype of termRegion
02/24/2003FemaleWhite1n/an/an/aInvoluntaryEurope
05/09/2005MaleAfrican American2n/an/an/aVoluntaryAsia
10/15/2007MaleHispanic3n/an/an/an/aNorth America

The problem is the date data is in different formats. Some are formatted as general m/d/yy, but some are formatted as custom: d/m/yy. If I change all the dates into one format I end up changing the months to days or vice versa.  I'd like to sort the table by the date format so I can just change one half of the dates to the other half's format. Any ideas? Thanks, Emma 

 

3 Replies

@Emma_Sabin 

Press Alt+F11 to activate the Visual Basic Editor.

Select Insert > Module.

Copy the following custom function into the code window:

Function NumberFormat(rng As Range)
    NumberFormat = rng.NumberFormat
End Function

Close the Visual Basic Editor.

In the first column next to the data, enter the following formula in row 2:

=NumberFormat(A2)

Fill down to the last row with data, for example by double-clicking the fill handle in the lower right of the cell with the formula.

Now sort the entire range (not just the new column!) on the new column.

Cells with the same number format will be grouped together.

how can you tell which date is formatted which if the date is 12 or less and the month is 12 or less? i.e 12/12/2007? or 9/12/2007? or 12/9/2007? How can you tell which number is the day and which number is the month and how do you decide how to sort them? by month? or by day?

@Emma_Sabin You need to convert the text date to an Excel date that can be sorted. (They count the number of days from about 1 jan 1900 and can thus be sorted correctly).

In the example, it is possible to identify the format as M/D/Y but as mentioned in another reply, that is not always the case.

The attached file converts quite a few date formats from text to date.

When ambiguous, it returns multiple alternatives in column B.

For your need, column O should come handy.

Insert an extra column to calculate the Excel date and then use it for sorting your data.