|
本帖最后由 hcy1185 于 2024-9-20 18:51 编辑
Sub MergeCells_xvhao() '有合并单元格
Dim ws As Worksheet
Dim rng As Range, Cell As Range
Dim i As Integer, rccount As Integer
Dim Startrow As Byte, hs As Byte, n As Byte '小于255的整数这样声明
Startrow = Cells.Find("序号", Cells(Rows.Count, 1), xlValues, xlWhole, xlByRows, xlPrevious).Offset(1).Row
Set rng = Range("A" & Startrow & ":A" & Cells(Rows.Count, "b").End(xlUp).Row)
rccount = 0
rng.ClearContents
For Each Cell In rng
If Cell.MergeCells Then
hs = Cell.MergeArea.Rows.Count '合并单元格内的行数
n = n + 1
If Not n = hs Then '合并单元格仅循环一次
GoTo JumpHuangChengyu
End If
' 执行下面的代码...
rccount = rccount + 1
Cell.Offset(-hs + 1).Value = rccount
Debug.Print Cell.MergeArea.Address
n = 0
JumpHuangChengyu:
' 跳转到这里执行下面的代码
Else
For i = 1 To Cell.MergeArea.Cells.Count
rccount = rccount + 1
Cell.MergeArea.Cells(i).Value = rccount
Next i
End If
Next Cell
End Sub
|
|