2 min readfrom Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

Sort range by column when first row is a month abbreviation

Our take

Sorting data with month abbreviations as headers can be streamlined with a clever approach using Excel functions. By generating a list of month numbers corresponding to abbreviations and temporarily adding it as a new row, you can effectively sort your data. This method utilizes the TEXT and DATE functions to create the month list, along with MATCH and SORT to organize the data seamlessly.

A little trick to sort a range of data when the header record is a month abbreviation. Any better approaches?

=LET(months,TEXT(DATE(1,ROW(A1:A12),1),"mmm"), data,A1:F4, DROP(SORT(VSTACK(MATCH(TAKE(data,1),months,0),data),1,1,1),1)) 

Here's how the trick works. We add a new record to the top of data that is a numerical month number corresponding to the month abbreviation of "dec", "nov", "apr", etc. To do this, you first need a list of all months which is created by:

TEXT(DATE(1,ROW(A1:A12),1),"mmm")

This says use the date() function to generate 12 dates in the year 1 for the first of each month and display them using the text() function with a parm of "mmm" which gives us the "jan", "feb", "mar", etc. list. This saves you from having to define a custom list in the excel options for use in the sort() function.

Then we use the match() function to bang the first row of data against this month list. So, "nov", "feb", "dec",... gets translated to 11, 2, 12,... etc. We temporarily add these month numbers as a new first row of the data range with (say this is our newdata):

VSTACK(MATCH(TAKE(data,1),months,0),data)

Then we use sort() to sort the whole thing by columns with the first row being the sort index (the month numbers). Then we have to drop that first temporary row to get back to our original data.

SORT(newdata,1,1,1)

This is easily adopted to sort a range by rows with the first column holding the month abbreviations.

https://preview.redd.it/4yjordqf3rtg1.png?width=1204&format=png&auto=webp&s=ab9c1a46fece578d261c941f15e70848f6b0fc2e

submitted by /u/wjhladik
[link] [comments]

Read on the original site

Open the publisher's page for the full experience

View original article

Tagged with