在 VBA 中,Range.Merge 方法用于合并指定的单元格区域。代码如下 Sub 合并单元格() Range('C3:C5').Merge End Sub 如果要取消合并的单元格,代码如下:
判断有没有合并单元格,代码如下 Sub 判断合并单元格() If Range('C3:C5').MergeCells Then 'Range对象的MergeCells属性判断有没有合并单元格 MsgBox '有合并单元格' Else MsgBox '没有合并单元格' End If End Sub 合并内容相同的连续单元格,代码如下
动画效果如下 取消合并单元格并保留内容,代码如下 Sub UnMerge() Dim StrMer As String Dim IntCot As Integer Dim i As Integer With Sheet1 For i = 2 To .Range('A65536').End(xlUp).Row StrMer = .Cells(i, 1).Value IntCot = .Cells(i, 1).MergeArea.Count .Cells(i, 1).UnMerge .Range(.Cells(i, 1), .Cells(i + IntCot - 1, 1)).Value = StrMer i = i + IntCot - 1 Next End With End Sub
|
|