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.

Beginner 15 min read

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.
ABCDE
1OrderRegionProductUnitsRevenue
21001NorthLaptop32850
31002SouthMonitor5945
41003NorthMouse20240
51004EastLaptop1950
61005SouthKeyboard12300
71006EastMonitor2378

Six orders. This list is used throughout the lesson.

Quick sort on one column

  1. Click any single cell in the column you want to sort by, for example a cell in Revenue. Do not select the column.
  2. Choose Data › Sort & Filter › Sort Largest to Smallest (the Z→A button). For text columns the buttons read A→Z and Z→A.
  3. Excel detects the whole list and the header row and sorts complete rows, so each order keeps its region and product.
Watch out. If you select only one column and sort it, Excel asks whether to Expand the selection. Always choose Expand. Continue with the current selection sorts that column alone and scrambles your data, separating each value from its row. If that happens, press 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.

  1. Click inside the list and choose Data › Sort & Filter › Sort.
  2. Make sure My data has headers is ticked.
  3. Sort by: Region, Sort On: Cell Values, Order: A to Z.
  4. Click Add Level. Then by: Revenue, Order: Largest to Smallest.
  5. Click OK.
ABCDE
1OrderRegionProductUnitsRevenue
21004EastLaptop1950
31006EastMonitor2378
41001NorthLaptop32850
51003NorthMouse20240
61002SouthMonitor5945
71005SouthKeyboard12300

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, West or Low, Medium, High instead 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.
Tip. Add an index column (1, 2, 3 …) before sorting a list whose original order matters, such as an import order. Sorting by that column later restores the original order.

AutoFilter

  1. 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.
  2. Click the arrow in Region. Untick Select All, tick North, click OK.
  3. Only orders 1001 and 1003 remain visible. The row numbers turn blue and the arrow shows a funnel icon.
  4. Filters on several columns combine with AND: filter Product = Laptop as well, and only order 1001 remains.
  5. 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 typeExamples of filters
NumbersGreater Than, Between, Top 10 (top n items or percent), Above Average
TextBegins With, Contains, Does Not Contain; wildcards * (any characters) and ? (one character)
DatesToday, This Month, Last Quarter, Year to Date, Between, and a year › month › day tree
AnyFilter 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:

E8fx=SUBTOTAL(109,E2:E7)
ABCDE
1OrderRegionProductUnitsRevenue
21001NorthLaptop32850
31002SouthMonitor5945
41003NorthMouse20240
51004EastLaptop1950
61005SouthKeyboard12300
71006EastMonitor2378
8Visible total5663

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.

Watch out. Copying a filtered range copies only the visible rows, but deleting or filling across a filtered range needs care: filling a formula down a filtered column writes only to visible cells in current versions, while pasting may land in hidden rows. Check the result after clearing the filter.

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:

HI
1RegionRevenue
2North>1000
3East

Criteria in H1:I3: (North AND Revenue > 1000) OR (East, any revenue).

  1. 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.
  2. Click inside the list and choose Data › Sort & Filter › Advanced.
  3. List range: $A$1:$E$7. Criteria range: $H$1:$I$3.
  4. Choose Copy to another location and enter K1 as Copy to, or leave Filter the list, in-place.
  5. Tick Unique records only if you want to drop duplicate rows. Click OK.
  6. Result: orders 1001, 1004 and 1006.
Note. In Microsoft 365 and Excel 2021 the FILTER and SORT functions do the same jobs with formulas that update automatically: =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?

2. Filters on Region = North and Product = Laptop are active at the same time. Which rows are shown?

3. Which formula totals only the visible rows of a filtered list?

4. In an Advanced Filter criteria range, conditions written on different rows are combined with…

5. Which keyboard shortcut toggles AutoFilter on Windows?

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 12 KB