Excel course · Lesson 9 of 34
Sorting and filtering data
Sort by one or many columns, sort by custom lists and colours, use AutoFilter with text, number and date filters, and the Advanced Filter.
Sorting and filtering are the first tools you reach for when a list grows past one screen. Sorting puts rows in a useful order: biggest customers first, oldest invoices at the top. Filtering temporarily hides the rows you do not need, so you can focus on one region, one month or one product. Both are safe to use as long as your data is laid out as a proper list, so we start there.
Prepare your data as a list
- One header row with a unique name in every column.
- One record per row, no blank rows or columns inside the data.
- Each column holds one kind of data: dates in a date column, numbers in a number column.
- No merged cells and no subtotal rows mixed into the data.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Order | Region | Product | Units | Revenue |
| 2 | 1001 | North | Laptop | 3 | 2850 |
| 3 | 1002 | South | Monitor | 5 | 945 |
| 4 | 1003 | North | Mouse | 20 | 240 |
| 5 | 1004 | East | Laptop | 1 | 950 |
| 6 | 1005 | South | Keyboard | 12 | 300 |
| 7 | 1006 | East | Monitor | 2 | 378 |
Six orders. This list is used throughout the lesson.
Quick sort on one column
- Click any single cell in the column you want to sort by, for example a cell in Revenue. Do not select the column.
- Choose Data › Sort & Filter › Sort Largest to Smallest (the Z→A button). For text columns the buttons read A→Z and Z→A.
- Excel detects the whole list and the header row and sorts complete rows, so each order keeps its region and product.
Ctrl+Z immediately.Sorting by several columns
To sort by region and, within each region, by revenue from high to low, use the Sort dialog.
- Click inside the list and choose Data › Sort & Filter › Sort.
- Make sure My data has headers is ticked.
- Sort by: Region, Sort On: Cell Values, Order: A to Z.
- Click Add Level. Then by: Revenue, Order: Largest to Smallest.
- Click OK.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Order | Region | Product | Units | Revenue |
| 2 | 1004 | East | Laptop | 1 | 950 |
| 3 | 1006 | East | Monitor | 2 | 378 |
| 4 | 1001 | North | Laptop | 3 | 2850 |
| 5 | 1003 | North | Mouse | 20 | 240 |
| 6 | 1002 | South | Monitor | 5 | 945 |
| 7 | 1005 | South | Keyboard | 12 | 300 |
Sorted by Region (A to Z), then Revenue (largest first).
Excel applies levels top to bottom: the first level is the main order, the next levels break ties. You can have up to 64 levels.
Custom order, colour and left-to-right sorting
- Custom list: in the Order box choose Custom List to sort by
North, South, East, WestorLow, Medium, Highinstead of alphabetically. - Sort On: Cell Color, Font Color or Conditional Formatting Icon: bring all red-filled cells to the top.
- Options › Sort left to right: sorts columns instead of rows, for example to reorder month columns.
- Options › Case sensitive: off by default.
AutoFilter
- Click inside the list and choose Data › Sort & Filter › Filter (
Ctrl+Shift+L, Mac:Cmd+Shift+F). A drop-down arrow appears in each header. - Click the arrow in Region. Untick Select All, tick North, click OK.
- Only orders 1001 and 1003 remain visible. The row numbers turn blue and the arrow shows a funnel icon.
- Filters on several columns combine with AND: filter Product = Laptop as well, and only order 1001 remains.
- To remove one filter, open its drop-down and choose Clear Filter From "Region". To remove all, choose Data › Sort & Filter › Clear.
Number, text and date filters
The drop-down offers filters suited to the data type:
| Column type | Examples of filters |
|---|---|
| Numbers | Greater Than, Between, Top 10 (top n items or percent), Above Average |
| Text | Begins With, Contains, Does Not Contain; wildcards * (any characters) and ? (one character) |
| Dates | Today, This Month, Last Quarter, Year to Date, Between, and a year › month › day tree |
| Any | Filter by Color, and the search box at the top of the list |
Example: open the Revenue drop-down, choose Number Filters › Greater Than, type 500 and click OK. Orders 1001, 1002 and 1004 remain.
Totals that respect the filter
SUM adds hidden rows too. To total only visible rows use SUBTOTAL or AGGREGATE:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Order | Region | Product | Units | Revenue |
| 2 | 1001 | North | Laptop | 3 | 2850 |
| 3 | 1002 | South | Monitor | 5 | 945 |
| 4 | 1003 | North | Mouse | 20 | 240 |
| 5 | 1004 | East | Laptop | 1 | 950 |
| 6 | 1005 | South | Keyboard | 12 | 300 |
| 7 | 1006 | East | Monitor | 2 | 378 |
| 8 | Visible total | 5663 |
Unfiltered, the total is 5,663. Filter Region = North and E8 shows 3,090.
Result: 5663
With the North filter applied, =SUBTOTAL(109,E2:E7) returns 3,090 (2,850 + 240) while =SUM(E2:E7) still returns 5,663. Function number 9 also ignores filtered rows; 109 additionally ignores rows you hid manually. AutoSum inserts SUBTOTAL automatically when a filter is active.
Advanced Filter
Data › Sort & Filter › Advanced handles cases AutoFilter cannot: OR conditions across different columns, and copying the results to another place. You type the criteria in a small range with the same headers:
| H | I | |
|---|---|---|
| 1 | Region | Revenue |
| 2 | North | >1000 |
| 3 | East |
Criteria in H1:I3: (North AND Revenue > 1000) OR (East, any revenue).
- Set up the criteria range: headers copied exactly from the list, conditions on the rows below. Conditions on the same row are AND; different rows are OR.
- Click inside the list and choose Data › Sort & Filter › Advanced.
- List range:
$A$1:$E$7. Criteria range:$H$1:$I$3. - Choose Copy to another location and enter
K1as Copy to, or leave Filter the list, in-place. - Tick Unique records only if you want to drop duplicate rows. Click OK.
- Result: orders 1001, 1004 and 1006.
=FILTER(A2:E7, B2:B7="North"). See Dynamic arrays: spill, FILTER, SORT, UNIQUE, SEQUENCE and #. A further option is the Sheet View feature for co-authored files, which lets each person filter without disturbing others (Collaboration: co-authoring, comments, notes and version history).Sorting and filtering in tables
If you convert the list to an Excel table (Ctrl+T), filter buttons appear automatically and the sort and filter range grows with new rows. Tables also offer Slicers, clickable filter buttons, under Table Design › Insert Slicer. See Excel tables and structured references.
Check yourself
1. You select only the Revenue column and click Sort. Excel asks about expanding the selection. What should you choose?
Only Expand keeps each row together. Sorting a single column scrambles the data.
2. Filters on Region = North and Product = Laptop are active at the same time. Which rows are shown?
AutoFilters on different columns combine with AND.
3. Which formula totals only the visible rows of a filtered list?
SUBTOTAL with 9 or 109 ignores rows hidden by a filter.
4. In an Advanced Filter criteria range, conditions written on different rows are combined with…
Same row = AND; different rows = OR.
5. Which keyboard shortcut toggles AutoFilter on Windows?
Ctrl+Shift+L turns the filter buttons on and off. Ctrl+L (or Ctrl+T) creates a table.