Can You Group Rows in Excel for Sorting? Here’s How

How-to basics · Updated November 18, 2026

Yes—but Excel’s Group command does not make rows stay together when you sort. To keep related records together, sort the entire dataset by a shared group or ID column, then add a second sort level for the order within each group. Use Data > Group only when you want rows to collapse or expand on screen.

Two different meanings of grouping rows

“Group rows” can mean either organizing records so they sort as a unit or using Excel’s outline feature to hide and reveal rows. These are different tasks, and confusing them can lead to scrambled data. Sorting changes the order of records according to values in one or more columns. Grouping with the ribbon creates an outline: rows can be collapsed beneath a numbered level or expanded again.

For example, a sales sheet might contain a customer name, invoice number, date, and amount on every row. If you want each customer’s invoices together, sort by customer name first and date second. If you want to hide invoice details beneath customer summaries, an outline may help—but it does not tell Excel to move all of a customer’s rows as one indivisible block during a sort.

What the Group command does

In Excel for Microsoft 365 on Windows, select the rows you want to be able to collapse, then choose Data > Group. Excel adds outline controls beside the row numbers. Use the minus button to hide the selected detail rows and the plus button to show them again. You can also use the outline-level buttons, usually numbered at the upper left of the sheet, to display different levels of detail.

This feature is useful for viewing a report at different levels, such as showing monthly totals while hiding the daily transactions beneath them. It is a display and navigation tool, not a sorting rule. If rows 2 through 5 are grouped, that does not mean Excel will keep them together when you sort by a value in another column.

  • Use Group to collapse or expand related rows visually.
  • Use Sort to reorder records according to column values.
  • Use a shared key, such as a customer ID, to keep related records adjacent after sorting.

To remove an outline, select its rows and choose Data > Ungroup. The exact ribbon layout can vary slightly with window size, but the Group and Ungroup commands are on the Data tab. On Excel for Mac, use the Data tab’s Group or Ungroup commands as well; menu and ribbon placement can differ from Windows.

Keep related records together with a multilevel sort

The dependable way to keep records together is to make the relationship explicit in the data. Put a group identifier in a column on every record, including repeated values where needed. Then sort by that identifier as the first level. Add a second level to order the records within each group, such as by date, invoice number, or priority.

For instance, suppose a project tracker has columns for Team, Task, Due Date, and Status. To show tasks together by team and then in due-date order, sort the complete range by Team, followed by Due Date. The Team value is the primary key for arranging the blocks; Due Date determines the order of rows inside each block. If there are two tasks for the same team and due date, add another level such as Task to make the order more predictable.

In Excel for Microsoft 365 on Windows, select a cell in the dataset and choose Data > Sort. In the Sort dialog, confirm that “My data has headers” is selected if the first row contains column names. Choose the first column in “Sort by,” set the order, and use Add Level to specify secondary and further sort columns. Click OK to apply the sort.

Before sorting, check that each record has all the values that define its group. A blank or inconsistent group ID can send a row to an unexpected part of the sorted list. If the group is described by more than one field—for example, Department and Region—sort by both fields in the desired priority order.

Sort complete records, not just one column

A row usually represents one complete record. Names, dates, amounts, and status values on that row belong together. If you select only the names column and sort it independently, the names can become detached from their associated dates and amounts. When Excel detects neighboring data, it may offer an “Expand the selection” choice. Choose that option so the other columns move with the sorted values.

A safer habit is to select a cell within the dataset and use Data > Sort, or convert a clean range into an Excel table with Insert > Table. When sorting a table, use a header’s filter arrow to choose a sort order; Excel reorders whole table rows rather than sorting just the visible values in that one column. Tables also make it easier to include new rows in the same structured dataset.

Keep the dataset rectangular: one header row, one record per row, and one field per column. Avoid blank separator rows, merged cells, and extra notes inside the sortable range. Those features can make Excel interpret the data as multiple separate regions or make a selection harder to identify. Put explanations outside the table, or in a dedicated Notes column, if they belong to individual records.

When a record has a parent and detail rows

Some sheets are not simple tables. A project may have one parent row followed by several indented task rows, or an order summary followed by separate product lines. If those rows must always travel together, sorting each line independently by a field such as date can split the parent from its details. An outline does not automatically define those relationships for the Sort command.

For a robust design, consider putting each detail on its own row and repeating the parent’s ID or name in a column. Then sort first by the parent ID and next by the detail’s desired order. For example, repeat Order ID 1042 on every product row belonging to that order, then sort by Order ID and Product Name. The repeated identifier allows Excel to place all lines for an order together without relying on blank parent rows or visual indentation.

If you must preserve a layout with separate parent and detail lines, add a helper column that assigns each row to its parent’s sort key and another that indicates its position within that group. Sort by the parent key first and the within-group position second. Test the approach on a copy before applying it to important data, especially if rows contain formulas or manual formatting. A normalized table with repeated IDs is generally easier to filter, sort, summarize, and use in PivotTables.

Common sorting problems and how to avoid them

  • Only one column moves: Undo immediately if the records become mismatched. Re-select the complete dataset and sort through Data > Sort, or use the table’s header dropdown.
  • Some rows do not move: Look for blank rows, blank columns, or a selection that stops before the last record. Include every row and every relevant column.
  • Groups appear in the wrong order: Confirm that the group field is the first sort level. A secondary level cannot keep groups together if the primary sort is a different field.
  • Numbers or dates sort oddly: Some values may be stored as text. Check their cell formats and underlying values, and make the entries consistent before sorting.
  • Hidden or collapsed rows are involved: Expand the outline and inspect the full range before sorting. Do not assume collapsed rows will behave like a protected block.

For a custom group order—such as High, Medium, Low rather than alphabetical order—use a Custom List in the sort settings or add a numeric rank column that represents the intended sequence. A rank field makes the desired order visible in the worksheet and can be easier to maintain when several people use the file.

Grouping, sorting, and subtotals compared

Choose the tool based on the outcome you need. Sorting arranges the records. Grouping creates collapsible outlines. Subtotals calculate summaries at changes in a selected field and can also create outline levels. These features can appear together in reports, but none should be treated as a substitute for a clear group key.

If you want totals for each department, sort the data by Department and then use Data > Subtotal, if that command is available for the selected range and workbook layout. Subtotal is designed for a sorted list and inserts summary rows; it is not the same as grouping ordinary records into permanent units. For a reusable summary that does not alter the source rows, a PivotTable is often a better fit: place the grouping field in Rows and a numeric field in Values.

In a table where users need to filter and sort frequently, keep source records in a simple table and build summaries separately. That separation reduces the chance that an inserted subtotal row or manually arranged block will interrupt later sorts.

Frequently Asked Questions

Can I group rows in Excel and then sort them as a block? Not with the Group command alone. Group creates collapsible outline levels. To keep records adjacent, sort the full dataset by a shared group ID or key, followed by any within-group sort fields.

How do I sort rows without separating their data? Select a cell in the complete dataset and choose Data > Sort, or sort from a table header dropdown. Include all related columns so each record stays on one row.

Can I sort only the visible rows in a collapsed group? Do not rely on a collapsed outline to define a safe sort range. Expand the rows first, verify the full dataset selection, and sort by the columns that express the desired order.

Can I keep a parent row with its detail rows? A visual outline alone does not guarantee that relationship during sorting. Repeating a parent ID on each detail row and sorting by that ID is more reliable. For a fixed layout, use helper sort keys and test on a copy.

Does sorting a table keep each record intact? Yes, when you sort using the table’s column controls or the Sort dialog with the complete table selected, Excel moves whole records. Check that related fields are inside the table and that the header row is recognized.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.