Excel guide · 6 min read
How to auto number rows in Excel
=ROW()-1 in A2 and fill it down: each row shows its position under the header, and the numbers close up when you delete a row. In Excel 365 or 2021, =SEQUENCE(COUNTA(B2:B100)) numbers every filled row in one formula. For fixed numbers that never change, type 1 and 2, select both and drag the fill handle.Sample data
Every result on this page was calculated from this sheet by the same spreadsheet engine the Cellwise app uses.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | No. | Guest | Table | Dietary |
| 2 | Priya Shah | 1 | Vegetarian | |
| 3 | Liam O'Connor | 1 | ||
| 4 | Sofia Russo | 2 | Gluten-free | |
| 5 | ||||
| 6 | Kenji Mori | 2 | ||
| 7 | Amara Okafor | 3 | Vegan | |
| 8 | ||||
| 9 | Lucas Meyer | 3 | ||
| 10 | Hannah Berg | 4 |
Download this sheet as an Excel practice file (.xlsx)
Method 1: the fill handle (fixed numbers)
The sheet is a guest list with two empty rows left for late additions. The quickest way to number it is to let Excel continue a pattern. The numbers it writes are plain values, not formulas, so they never change on their own.
Double-clicking the fill handle fills down as far as the column next to it has data, and it stops at the first gap. Here it stops at row 4 because row 5 is empty, so drag instead when your list has holes.
- Type 1 in A2 and 2 in A3.
- Select A2:A3.
- Drag the small square at the bottom-right corner of the selection (the fill handle) down to the last row, or double-click it.
- For a long list, type 1 in A2, select it, then go to Home > Fill > Series, choose Columns, set Step value 1 and Stop value 500, and click OK.
Method 2: ROW()-1 (renumbers when you delete)
ROW() returns the row number of the cell it is in. The header takes row 1, so =ROW()-1 in A2 gives 1, in A3 gives 2, and so on. Fill it down once and you are done. Delete a row and every number below moves up by one, with no gap.
If the header is not in row 1, subtract the header's row instead: =ROW()-ROW($A$1). That keeps working if someone later inserts rows above the table, because the $A$1 reference moves with the header.
The catch is inserted rows. A new row has no formula, so it stays empty and the next row's number skips ahead until you copy the formula into it. Turning the list into a table (Insert > Table) fixes this: a formula typed in a table column fills itself into every new row.
Result: 5
5. ROW(A6) is 6, the row number of A6. Typed in A6 itself, =ROW()-1 returns the same 5.
Method 3: SEQUENCE and COUNTA (Excel 365 and 2021)
SEQUENCE(n) spills the numbers 1 to n down the column, and COUNTA counts the filled names, so =SEQUENCE(COUNTA(B2:B100)) in A2 grows by itself as you add guests. Point COUNTA at a range longer than the list so new names are counted.
It only lines up when the list has no gaps. The guest list has 7 names over 9 rows, so the spill writes 1 to 7 into A2:A8. The empty row 8 gets a 7, and Hannah in row 10 gets no number at all. Use this method on lists without blank rows, and keep the cells below A2 empty or you get #SPILL!. Spilled formulas do not work inside an Excel table either.
For a numbered copy of the list with the gaps removed, Excel 365 users can spill both columns at once: =HSTACK(SEQUENCE(COUNTA(B2:B10)),FILTER(B2:B10,B2:B10<>"")).
Result: spills 7 rows × 1 column
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
Spills 1 to 7 into A2:A8. Count the names, not the rows: blank rows push the numbers out of line.
Number only the filled rows (skip blanks)
To leave empty rows unnumbered and keep counting on the next name, test the row first and count the names so far. Put this in A2 and fill it down to A10: =IF(B2="","",COUNTA($B$2:B2)). The range starts at a locked $B$2 and grows one row at a time, so each row shows how many names appear from the top down to it.
On the guest list that gives 1, 2, 3, blank, 4, 5, blank, 6, 7. A cell that holds only a space looks empty but is not, so it gets a number and is counted. Clear stray spaces (Home > Find & Select > Replace, a space with nothing) before you trust the result.
Result: 4
4: Kenji is the fourth guest. Row 5 above him is empty, so it shows nothing and is not counted.
Numbers that survive sorting and deleting
Decide first what the number means. If it is the row's position (1 at the top, always in order), use ROW()-1: after you sort by name, the numbers still read 1, 2, 3 from the top, but they no longer belong to the same guests.
If it is an ID that must stay with the record, such as a ticket or guest number, it has to be a fixed value. Number the list with the fill handle, or with a formula and then convert it: select the numbers, copy, and paste back with Home > Paste > Paste Values. Include column A when you sort and the IDs travel with their rows. Deleting a row then leaves a gap, which is usually what you want for an ID.
Avoid the =A2+1 chain (1 in A2, =A2+1 in A3 and so on). It looks fine until you delete a row: the formula below loses its reference and shows #REF!, and every row under it shows #REF! too.
For a filtered list, =SUBTOTAL(3,$B$2:B2) filled down counts only the visible names, so the numbers stay 1, 2, 3 on screen when you filter. The app's engine cannot run SUBTOTAL, so there is no live result here.
Which one to pick
Permanent IDs that move with sorting: the fill handle or pasted values. A position number that closes up after deletes: ROW()-1, inside a table if people insert rows. A list that grows with no gaps in Excel 365: SEQUENCE(COUNTA()). Blank rows in the list: the IF and COUNTA formula. To count repeats of the same value (1st Acme order, 2nd Acme order), use a running count with COUNTIF instead.
Common mistakes
Double-clicking the fill handle on a list with gaps
It stops at the first empty cell in the neighbouring column. Drag down, or use Home > Fill > Series with a stop value.
Dragging a single 1 and getting 1, 1, 1
With one cell selected, Excel copies it. Type 1 and 2 first and select both, or hold Ctrl while you drag, or pick Fill Series from the Auto Fill Options button that appears after the drag.
Using ROW()-1 as a permanent ID
ROW()-1 shows the position, so sorting reassigns the numbers to different records. Paste the numbers as values before you sort.
Leaving data under a SEQUENCE formula
A spill needs every cell it will fill to be empty. Clear the cells below, or you get #SPILL!.
FAQ
How do I automatically number rows in Excel?
Type =ROW()-1 in the first data row (when the header is in row 1) and fill it down. The numbers update when you delete rows. In Excel 365 or 2021, =SEQUENCE(COUNTA(B2:B100)) numbers every filled row of column B in one formula.
How do I number rows in Excel without dragging?
Type 1 in the first cell, then use Home > Fill > Series, choose Columns, and enter a stop value. Or use =SEQUENCE(500) in Excel 365 or 2021, which spills 1 to 500 at once.
How do I keep row numbers after sorting in Excel?
Formula numbers based on ROW() always read 1, 2, 3 from the top, so they do not stay with a record. Convert them to fixed values with Paste Values, then include the number column when you sort.
How do I auto number only rows that have data?
Use =IF(B2="","",COUNTA($B$2:B2)) and fill it down. Empty rows stay blank and the count continues on the next filled row.
How do I make numbers like G-001, G-002?
Join a prefix to a formatted number: ="G-"&TEXT(ROW()-1,"000"). Paste the result as values if the codes are IDs that must never change.
Functions used
ROWRow number of a cellSEQUENCENumbered list, spillsCOUNTACounts non-empty cellsIFPicks a result from a testFILTERKeeps matching rows, spillsHSTACKJoins arrays side by sideTEXTNumber to formatted text
Related guides
- Running count of occurrences in Excel
- COUNT vs COUNTA vs COUNTBLANK
- How to remove blank rows in Excel
- #SPILL! error in Excel
- Excel keyboard shortcuts for beginners
- How to reverse the order of a list in Excel
- How to transpose rows to columns in Excel
Updated