Detailed explanation of how to automatically fill in number sequences in merged cells

How can we populate serial numbers into a merged list of cells of different sizes in Excel? Our first thought is to drag the AutoFill handle to fill the merged cells, but in this case, we will receive the following warning message and the merged cells will not be filled.

Is there a way to number merged cells without having to type the numbers manually?

For example, A1 is the header and the range A2:A15 consists of merged cells of different sizes, see the screenshot below. How to fill in the numerical sequence in A2:A15?

Use Excel formulas

1. Select the merged cells (in this example, select A2:A15).

2. Press the F2 key, or click the formula bar and enter the formula: =MAX(A$1:A1)+1.

3. Press CTRL+ENTER.

Using VBA code

The following VBA code can help you quickly number selected merged cells. Please do the following:

1. Press and hold the ALT+F11 keys, it will open the Microsoft Visual Basic for Applications window.

2. Click "Insert" > "Module" and paste the following code in the "Module Window".

Sub NumberCellsAndMergedCells()
  Dim Rng As Range
  Dim WorkRng As Range
  On Error Resume Next
  xTitleId = "Number For Merged Cells"
  Set WorkRng = Application.Selection
  Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type: = 8)
  Set WorkRng = WorkRng.Columns(1)
  xIndex = 1
  Set Rng = WorkRng.Range("A1")
  Do While Not Intersect(Rng, WorkRng) Is Nothing
    Rng.Value = xIndex
    xIndex = xIndex + 1
    Set Rng = Rng.MergeArea.Offset(1)
  Loop
End Sub

3. Then press the F5 key to run this code, a prompt box will pop up allowing you to select the merged cells you want to fill, see screenshot:

4. After selecting the merged cells, click "OK". Now, the merged cells you selected have been filled with serial numbers, please see the screenshot:

Guess you like

Origin blog.csdn.net/wyxtx/article/details/132874387