Except sort data by “A to Z” (alphabetical order, for numbers from small to large), we can also sort data by date, month or year if these conditions are given. In this article, we will show you the way to sort data by day of week.
Table of Contents
1. Sort Date by Day of Week using Sort & Filter Feature
->
In the left table, sales are sorted by date. Now, we want to sort them by day of week, see the table on the right side for example. Obviously, we cannot sort them just by clicking “Sort A to Z”, in this way day of week values will be sorted by alphabetical order. To sort data by proper order as we expected, we can create a customer list with the help of Sort->Custom List function.
RESOLUTION
Excel Sort module provides several sorting rules for users to sort data. Except normal “A to Z” or “Z to A”, it also provides orders like by month or by day of week. Users can also create custom list depends on requirement.
STEPS
a. Select table A2:C15 ignoring the headers, click Data in the ribbon, click Sort button in Sort & Filter section.
b. Sort dialog pops up. In Column sort by dropdown list, select column “Day of Week”. Select the column you want to sort by in this step, as we want to sort data by day of week column, so we select it.
Keep Sort On selection unchanged, in Order dropdown list, select “Custom List”.
c. After selecting “Custom List”, “Custom Lists” dialog pops up. In Custom lists panel, select “Sunday, Monday…”, then day of week values are listed in the right panel with expected order, they are sorted from Sunday to Saturday.
d. Click OK on this dialog. We return to Sort dialog. Now in Order dropdown list, order “Sunday, Monday, Tuesday…” is displayed. Click OK to apply this setting for the selected table.
e. Now sales are sorted by day of week properly. The same values of day of week are grouped.
COMMENT
- If we select day of week abbreviations like “Sun, Mon, Tue…” in “Custom lists” to sort data, data is sorted improperly, Friday is listed on top.
- If selected range with headers included, we need to check on “My data has headers” when applying sorting settings. See screenshot below. This option is checked by default.
- If you want to sort day of week by another order, for example from Monday to Sunday, you can just enter a new list in “List entries” panel. Enter “Monday” as the first entry, press Enter to separate list entries.
After creating your list, click Add button to add the new list to custom list. Then you can sort values by this new list properly.
2. Sort Date by Day of Week using VBA Code
Now, moving on to the second method, we’ll explore the power of VBA code to automate the sorting of dates by day of the week. This method provides advanced customization options, allowing you to tailor the sorting process to suit your specific requirements.”
Press ‘Alt + F11‘ to open the Visual Basic for Applications editor.
Right-click on any item in the project explorer , hover over “Insert,” and select “Module” to add a new module.
In the newly created module, copy and paste the following VBA code:
Sub SortRowsByDayOfWeek()
Dim rng As Range
Dim sortRange As Range
Dim lastRow As Long
' Prompt the user to select the range containing day of the week values
On Error Resume Next
Set sortRange = Application.InputBox("Select the range containing day of the week values:", Type:=8)
On Error GoTo 0
If sortRange Is Nothing Then
MsgBox "No range selected. Sorting aborted."
Exit Sub
End If
' Get the last row in the selected range
lastRow = sortRange.Rows.Count
' Sort the rows by the day of the week values
With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=sortRange, _
SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday", _
DataOption:=xlSortNormal
.SetRange ActiveSheet.Range("A1").CurrentRegion ' Assuming your data starts from cell A1
.Header = xlYes ' Change to xlNo if your data doesn't have headers
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Close the VBA editor and return to your Excel workbook.
Press ‘Alt + F8’ to open the “Macro” dialog box.
Select the macro named “SortDatesByDayOfWeek“
and click “Run“
When prompted, select the range of cells you want to sort based on the dates.
click ok button.
The VBA code will dynamically sort your dates by day of the week based on the selected source cell.
3. Video: Sort Date by Day of Week
This Excel video tutorial where we’ll explore two methods to sort dates by day of the week. Join us as we dive into the first method using Excel’s Sort & Filter feature, followed by the second method utilizing VBA code.