1、合并内容相同的连续单元格
如果需要合并如图所示的工作表中B列中部门相同的连续单元格
VBA代码:
cpp
Sub Mergerng()
Dim IntRow As Integer
Dim i As Integer
Application.DisplayAlerts = False
With Sheet1
IntRow = .Range("A65536").End(xlUp).Row
For i = IntRow To 2 Step -1
If .Cells(i, 2).Value = .Cells(i - 1, 2).Value Then
.Range(.Cells(i - 1, 2), .Cells(i, 2)).Merge
End If
Next
End With
Application.DisplayAlerts = True
End Sub
2、取消合并单元格时在每个单元格中保留内容
如果需要合并如图所示的工作表中B列中部门相同的连续单元格
VBA代码:
cpp
Sub UnMerge()
Dim StrMer As String
Dim IntCot As Integer
Dim i As Integer
With Sheet1
For i = 2 To .Range("B65536").End(xlUp).Row
StrMer = .Cells(i, 2).Value
IntCot = .Cells(i, 2).MergeArea.Count
.Cells(i, 2).UnMerge
.Range(.Cells(i, 2), .Cells(i + IntCot - 1, 2)).Value = StrMer
i = i + IntCot - 1
Next
End With
End Sub
关注
笔者 - jxd