keepcalculator

06 Numbers and Powers

Conditional Formatting Last N Rows

Card No. 06·48977 · Updated · Standard formula, shown below

First highlighted row 96 row number
Last highlighted row
100 row number
Result
Number of rows to highlight
5 rows
You entered
Total rows in data range
100 rows
You entered
You enter
Number of rows to highlight, Total rows in data range
You get
First highlighted row, Last highlighted row
Rounding
0 dp

Conditional formatting for the last N rows in Excel or Google Sheets highlights the bottom N rows of a dataset based on their position, regardless of the values they contain. You enter a formula that compares each row’s relative position to the total number of rows, and the formatting is applied automatically as your data changes. This guide explains the exact formulas for both Excel and Google Sheets, how to adapt them for filtered or sorted data, and common pitfalls to avoid.

Formula

The general formula to highlight the last n rows of a range is:

For Excel (applied to the range A1:E100):

=ROW()>=COUNTA($A:$A)-n+1

Where:

  • ROW() returns the row number of the current cell (e.g., 1 for row 1).
  • COUNTA($A:$A) counts the number of non-empty cells in column A – this represents the total number of rows with data.
  • n is the number of rows you want to highlight (e.g., 5).

For Google Sheets, the same formula works, but you might prefer to use =ROW()>=COUNTA(A:A)-n+1 without the dollar signs if you want the range to auto-expand.

If your data has blank rows or you want to use a specific range (e.g., A1:E100), replace COUNTA($A:$A) with ROWS($A$1:$A$100) to get the total row count directly.

Worked Example

Suppose you have a dataset in rows 1 to 100 (100 rows) and you want to highlight the last 5 rows (rows 96 to 100).

Using the formula =ROW()>=COUNTA($A:$A)-5+1:

  1. Count non-empty cells in column A: 100.
  2. Calculate the starting row: 100 – 5 + 1 = 96.
  3. So the formula becomes =ROW()>=96.
  4. For any cell in row 96, ROW() returns 96, which is ≥ 96 → TRUE, so it gets formatted.
  5. Rows 97–100 also return TRUE. Rows 1–95 return FALSE, so they are not formatted.

The result: exactly rows 96 to 100 are highlighted, matching the calculator output (start_row = 96, end_row = 100).

How to Apply in Excel

Follow these steps to set up conditional formatting for the last N rows in Excel:

  1. Select the entire data range (e.g., A1:E100). Make sure the top-left cell is the active cell (usually A1).
  2. Go to the Home tab, click Conditional Formatting > New Rule.
  3. Choose Use a formula to determine which cells to format.
  4. Enter the formula =ROW()>=COUNTA($A:$A)-5+1 (replace 5 with your desired N).
  5. Click Format, choose a fill color, and click OK.
  6. Click OK again to apply the rule.

Important: The formula must be entered relative to the active cell. If you select A1:E100, the formula uses ROW() without a row number, which Excel interprets relative to each cell in the selection.

How to Apply in Google Sheets

In Google Sheets, the process is similar:

  1. Select the range (e.g., A1:E100).
  2. Click Format > Conditional formatting.
  3. Under “Format rules,” choose Custom formula is.
  4. Enter =ROW()>=COUNTA(A:A)-5+1 (adjust N as needed).
  5. Set the formatting style (e.g., fill color) and click Done.

One difference: Google Sheets automatically applies the formula to the entire selected range, so you don’t need to worry about absolute vs. relative references as much.

Choosing the Right N

How do you decide how many rows to highlight? It depends on your goal:

  • Recent entries: If your data is sorted by date, highlighting the last 7 rows might show the last week’s entries.
  • Top N by position: If you want to mark the last 10% of rows, calculate N as =ROUND(COUNTA(A:A)*0.1,0) and use that in the formula.
  • Dynamic N: You can put N in a cell (e.g., $B$1) and reference it: =ROW()>=COUNTA($A:$A)-$B$1+1.

Always ensure N is less than or equal to the total number of rows; otherwise, the formula will highlight all rows.

Common Problems and Fixes

Here are frequent issues users encounter and how to solve them:

  • Wrong rows highlighted: Usually caused by not adjusting the formula for the starting row. If your data starts at row 5, use =ROW()>=COUNTA($A:$A)-n+5 instead.
  • Blank cells in column A: COUNTA counts non-empty cells, so if column A has blanks, the count will be off. Use ROWS($A$1:$A$100) if you know the exact range.
  • Filtered data: Conditional formatting ignores filters – it highlights based on the original row numbers, not visible rows. To highlight visible last N rows, you need a more complex formula using SUBTOTAL or AGGREGATE.
  • Sorted data: If you sort your data, the “last N rows” will move with the sort. That’s usually what you want, but if you want to track specific records, use a helper column with a unique ID.
  • Entire row vs. cell range: To highlight the entire row, apply the rule to the whole row range (e.g., A1:E100) and use a formula that references the first column: =$A1>="" combined with the row condition.

Alternatives and Advanced Techniques

If the simple formula doesn’t fit your needs, consider these alternatives:

  • Use a helper column: Add a column with a formula like =ROW() and then use conditional formatting based on that column’s value.
  • Highlight last N visible rows: Use =SUBTOTAL(3,$A$2:$A2)>=MAX(SUBTOTAL(3,$A$2:$A$100))-n+1 in a helper column, then format based on that helper.
  • Use a table: Excel Tables (and Google Sheets’ “Alternating colors” feature) have built-in row banding, but not specifically for last N rows.
  • VBA (Excel only): For a one-time static highlight, you could use a macro, but the formula method is simpler and dynamic.

For most users, the standard formula is sufficient. It’s dynamic, updates automatically when you add or remove rows, and doesn’t require any scripting.

Frequently Asked Questions

Why is my formula highlighting the wrong rows?

This usually happens because the formula’s row reference is not relative to the active cell. When you apply the rule, the formula should be written as if it’s for the top-left cell of the selected range. For example, if you select A1:E100, the formula =ROW()>=COUNTA($A:$A)-5+1 works because ROW() returns the row of each cell. If you start the selection at row 3, you need to adjust the formula to =ROW()>=COUNTA($A:$A)-5+3.

Can I highlight the last N rows based on a condition, like only rows where status is “Pending”?

Yes, you can combine conditions. For example, to highlight the last 5 rows where column B equals “Pending”, use =AND(ROW()>=COUNTA($A:$A)-5+1, $B1="Pending"). This highlights only the rows that meet both criteria.

Does this work with Excel Tables?

Yes, but you need to adjust the formula to use structured references. For a table named Table1, you can use =ROW()>=ROWS(Table1)-5+1. The ROWS function returns the number of rows in the table, and the formula works the same way.

How do I highlight the last N rows in a filtered list?

The standard formula doesn’t respect filters. To highlight the last N visible rows, you need a helper column with a formula that assigns a sequential number to visible rows only. For example, in a helper column, use =SUBTOTAL(3,$A$2:$A2) and then format based on that helper column’s maximum value minus N.

Will the formatting update automatically when I add new rows?

Yes, if you use COUNTA on a whole column, it will count the new rows automatically. However, if you used a fixed range like ROWS($A$1:$A$100), you’ll need to update the range. To avoid this, use COUNTA with a full column reference, or use a dynamic named range.

Related

Formula used
in n | Number of rows to highlight | rows | 5 | 1 | 1000
in total | Total rows in data range | rows | 100 | 1 | 100000
out start_row | First highlighted row | row number | total - n + 1 | 0
out end_row | Last highlighted row | row number | total | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →