How to Automatically Generate Serial Numbers in Excel with Merged Cells and Horizontal Tables

When a table has merged cells or is laid out horizontally, serial numbers tend to break off or fall out of place. This article gives formula approaches that do not depend on the merged areas, covering three cases: vertical layout, horizontal layout, and merged cells in the serial-number column itself, and explains how to verify the numbering after sorting and filtering.

Key points of this article

  • Do not put serial numbers inside merged regions. Instead, count based on an unmerged data column, so merged cells will not interrupt the numbering.
  • For vertical tables, use formulas such as =IF(C2="","",COUNTA(C$2:C2)), so that serial numbers only appear on rows that have content.
  • For horizontal tables, place the serial numbers in separate, unmerged rows and count rightward across columns using COLUMNS or COUNTA.
  • After filtering, use SUBTOTAL(103,…) to count, so that serial numbers are consecutive only for visible rows.
  • After sorting or inserting rows, check each item one by one. If gaps or duplicate numbers appear, first check whether the counting range includes merged areas or blank rows.

One common headache when building tables is that the serial numbers suddenly break near merged cells: after dragging the fill handle, some rows turn blank and others repeat the same number. This problem usually stems from the table's structure rather than from Excel's numbering feature itself. Adding group headings to similar items and merging cells of the same category is a very common practice in reports, but merged areas interfere with filling and counting.

This article is for people who need to organize reports, name lists, purchase lists, or project schedules. It covers two common layouts: first, a vertical table with merged category columns; second, a table laid out horizontally where the serial numbers must be numbered from left to right. After reading it, you will be able to use a set of formulas to generate serial numbers that are not affected by merged cells, and you will know how to check the results.

Merged cells and landscape tables: how do you generate automatic numbering? One-sentence answer

Serial numbers should be counted from a column of unmerged data, rather than filled in by the merged area itself. For vertical tables, use a counting formula; for horizontal tables, count by column. Do not merge serial number columns or serial number rows.

Your situationRecommended PracticesCause
Vertical table, category column merged, each row has specific itemsUse COUNTA to count the item column, showing the number only in rows that have content.Items are not merged; counts are not affected by category merging.
The serial number column itself should be merged by groupWrite the MAX formula only in the top-left cell of each group, leaving the other cells in the group blank.The merged region takes its value only from the first cell, and subsequent groups can still continue the numbering.
The table should be arranged horizontally, and the numbering should run from left to right.Put the serial number in its own row, and use COUNTA or COLUMN to count to the right.References change with the column during horizontal fill, without relying on merged cells.
After filtering, the numbering must be consecutive.Switch to COUNTA-style counting with SUBTOTAL(103, …)Count only visible rows; hidden rows do not take up a number.
The data is frequently sorted or has rows added and deleted.Use formula numbering, not manually typed numbersFormulas automatically recalculate when the positions of their referenced cells change.

Before You Begin: Confirm These Points

Determine the table structure before you start, and the formula syntax will become much clearer.

  • Confirm which column is the "real data column." This should be a column where every row has content, such as the project name, person's name, or product number. It is used to determine whether a row needs a number. If this column also contains merged cells, unmerge them first or change them so each row is filled in; otherwise the count will be wrong.
  • Confirm where the serial numbers should go. In a vertical table, they usually go in a column on the left; in a horizontal table, they usually go in a row above the header. Either way, do not merge cells in the row or column that holds the numbers, or gaps will appear when filling.
  • Confirm whether the table will be filtered or sorted. If filtering is needed, switch the counting formula to SUBTOTAL; if the table is only being organized once, the plain COUNTA is sufficient.
  • Confirm the data source. If the table comes from a PDF or Word file, you can first export it to Excel using the PDF editor in the OmniDoc desktop client, then process it using the methods below. The download address for the desktop client is download.omnidoc.top.
  • Confirm your Excel version. Newer functions such as SEQUENCE require a recent version. Older versions can use only the COUNTA and SUBTOTAL approaches described in this article, which offer better compatibility.

Steps to generate serial numbers in a vertical table that are unaffected by merged cells

The following example assumes: Column A is "Category" and contains merged cells; Column B is "Serial Number"; Column C is "Item Name", with content in every row, starting from row 2.

  1. Unmerge the serial number column. Select the range in column B and confirm that every cell is independent. Merged serial number cells will break subsequent filling, so keep each serial number cell as a single cell.
  2. Enter the counting formula in B2. Type =IF(C2="","",COUNTA(C$2:C2)). The formula means: if the item in this row is empty, show blank; otherwise, count the filled cells in the item column from row 2 down to this row.3. Fill down. Drag the fill handle of B2 to the last row of the table. Because C$2 fixes the starting point, the counting range expands row by row as you drag down.
  3. Check that merged categories do not affect the result. The merged regions in category column A have no effect on the formula, because the formula only looks at column C. If a row in column C is empty, its corresponding serial number will also be left blank, indicating that this row is not an item to be numbered.
  4. To renumber by group, write a MAX formula in the first cell of each group. If you want the serial number column merged by group, such as merging each category into one serial number, write =MAX(B$1:B1)+1 in the top-left cell of that group's merged area. MAX ignores text headers and takes the largest existing number above, then adds one. Do not write formulas in the other cells within the group.
  5. Use Ctrl+Enter or confirm in sections. If you only want to enter the formula in a specific area, first select the area, type the formula, and press Ctrl+Enter so that all selected cells receive the same relative-reference result at once.
  6. Delete test rows and confirm no leftover numbers remain. If there are blank rows in between, their corresponding serial numbers should be left blank and should not display old numbers.

How to fill in serial numbers in a horizontal table

Horizontal tables usually have their serial numbers in a row above the header row. Suppose row 1 holds the serial numbers, row 2 is the header row, and the data starts in column B and runs to the right.

  1. Enter the formula in B1 of the serial-number row. Type =IF(B2="","",COUNTA($B$2:B2)). The formula counts the non-empty cells from column B up to the current column in the header row.
  2. Fill it to the right. Drag the fill handle of B1 right to the last column of the table. The references shift with each column, so every column gets the count of non-empty cells before it.
  3. If you don't want to count by row, use the column number. If every header cell has content, you can also write =COLUMNS($B$2:B2), which gives the same result, but when there is an empty column in the middle it numbers consecutively without skipping. Which one to choose depends on whether you want to skip empty columns.
  4. Avoid merging cells in the serial-number row. If there are group headings above the header row, put the group headings in another row, and keep the serial-number row completely unmerged.
  5. Before hiding the serial-number row, confirm it is not used in other calculations. Hidden rows do not affect the result of COUNTA, but if you later filter the whole table, you should switch to SUBTOTAL for counting.

Verify item by item after completion

Auto-generated sequence numbers from formulas usually look fine, but after sorting, filtering, or inserting rows, you still need to check them.

Inspection itemHow do you see it?What should I do if I have a problem?
Are the serial numbers consecutive?Looking down from the first data row, the numbers increase by one each time, with no duplicates or gaps.Check the data column for spaces or text that looks empty, clear them, and then re-fill the formulas.
Should blank lines be left empty?For rows with no items in between, the serial number should display as blank.Check whether column C in this row is really empty; if a space was entered, just delete the space.
Does the merged area cause interference?Click the sequence number cell and check whether the formula references a merged cell.Change the counting range to unmerged data columns, and do not reference any cells other than the first cell of the merged area.
Is it continuous after filtering?Show only some rows, and check whether the serial numbers start from 1 and are consecutiveChange COUNTA to SUBTOTAL(103, …), then refill
Is it correct after sorting?Sort the items in the column, then check whether the serial numbers are recalculated along with the rows.If the sequence number turns into a fixed number, it means it is not a formula, and the formula needs to be rewritten.
Are the horizontal serial numbers aligned?Check whether each number in the sequence-number row matches the data in its corresponding column.Check whether the serial number row has merged or hidden columns, then refill starting from the first column.

🎯 Which Approach Suits You?

  • Reports that are made only once and don't need sorting: a plain COUNTA formula is enough; write it once and it can be used.
  • Tables that are often filtered and viewed by department or status: use SUBTOTAL(103, …) so that the numbering applies only to visible rows.
  • Tables with many categories where each category needs its own numbering: group and merge the serial-number column, write a MAX formula in the top-left cell of each group, and leave the rest of the group blank.
  • Tables that come from scanned images or PDFs: first convert the content to Excel, then handle the serial numbers; if the converted data is misaligned, correct the data columns first, otherwise the serial numbers will also be wrong.

❓ Frequently Asked Questions (FAQ)

1. Why does the numbering break after merging cells?

When you drag the fill handle, a merged area keeps only the value in its top-left cell while the other cells are empty, so the fill result shows blanks or duplicates. Put the sequence numbers in an unmerged column and count using a data column, and this problem can be avoided.

2. Does COUNTA count spaces too when counting?

Yes. A cell containing only a space is counted as non-empty by COUNTA. When checking the data column, you can use Find to search for the space and replace it with nothing, then refill the formula.

3. Can the sequence numbers be entered manually instead of using formulas?

Yes, but it only works well for tables whose contents won't change. Once you sort, insert, or delete rows, manually entered numbers won't adjust automatically, so using formulas is safer in that case.

4. Why do the sequence numbers still skip after filtering? Where did I go wrong?

Most likely you're still using COUNTA or COLUMN to count. COUNTA also counts hidden rows, so after filtering the numbers look like they skip. Change the formula to SUBTOTAL(103, …), which counts only visible non-empty cells, and the sequence numbers will become continuous again.

5. Do the generated serial numbers need to be checked manually?

Formulas can resolve most issues with skipped and duplicate numbers, but if the data column itself contains errors, duplicates, or content that was accidentally deleted, the sequence numbers will simply reflect those problems faithfully. Review the results item by item before publishing.

📝 Summary

Merged cells and horizontal layouts do not necessarily make serial numbers complicated. Put the serial numbers in separate, unmerged columns or rows, count based on a data column, and use COUNTA for vertical tables or column counting for horizontal tables, so that serial numbers in an Excel table are generated automatically and consecutively.

Open OmniDoc and process your spreadsheet file

Updated 2026-11-29