Highlight Where a Group Changes

Excel Formulas › Conditional Formatting

All versionsComparison

Draw a line between groups in sorted data — a top border (or shading) on the first row of each new category. A rule comparing each row to the one above marks the boundaries.


Quick formula: on sorted data, select the range and add a rule:
=$A2 <> $A1
TRUE when this row’s group differs from the row above — i.e. the first row of a new group. Apply a top border in the format.

The example

First row of each region group is marked.

AB
1RegionRep
2EastAnn
3West (new)Cy

The formula

Compare each row to the one above:

=$A2 <> $A1 // first row of each new group

How it works

A change in the key marks a boundary:

  1. Make sure the data is sorted by the grouping column.
  2. Add a rule =$A2 <> $A1 — TRUE when the current group differs from the previous row.
  3. In the format, add a top border (or a fill) to draw the separator at each group start.
  4. Lock the column with $A and select all columns so the border spans the whole row.

Separators, not bands. This marks the boundary between groups with a line; to shade alternating groups in different colors, use the group-counter approach in the shade-alternating-groups recipe.

Try it: interactive demo

Live demo

Sorted groups; new groups get a top line.

Variations

Shade the new row

Fill instead of border:

=$A2 <> $A1 (fill format)

Alternating group colors

Use a group counter — see shade-alternating-groups.

Subtotal boundaries

Mark where to insert subtotals.

Pitfalls & errors

Sort first. Boundaries only make sense on data grouped together; unsorted data marks every change.

First data row. Row 2 compares to row 1 (the header) — it’ll always mark the first group, which is usually fine.

Border in the format. The top-border look is set in the rule’s Format, not the formula.

Practice workbook

📊
Download the free Highlight Where a Group Changes practice workbook
A group-boundary CF rule with the shade and subtotal variants, plus 4 challenges with answers. No sign-up required.

Frequently asked questions

How do I draw a line between groups in Excel?
On sorted data, add a formula CF rule =$A2 <> $A1 and give it a top border. It marks the first row of each new group.
What's the difference from banded groups?
This marks the boundary with a line; banding shades whole groups in alternating colors (use a group counter for that).
Why is every row marked?
The data isn't sorted by the group column, so the group changes on almost every row. Sort first.

Stop fighting formulas. Learn them in a day.

This recipe is one of hundreds of real-world formulas we teach. Our Excel Formulas & Functions class covers lookups, logic, text, and dynamic arrays hands-on — live in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.

See the Formulas & Functions Class

Related formulas: Shade alternating groups · Subtotal visible rows · Highlight entire row