|
3列相同数据关联合并
Private Sub CommandButton1_Click()
Dim xRow As Integer
Dim i As Integer
Dim a As Integer
xRow = Range("A1").CurrentRegion.Rows.Count
a = 0
For i = 1 To xRow
If Cells(i + 1, 1) = Cells(i - a, 1) And Cells(i + 1, 2) = Cells(i - a, 2) Then
a = a + 1
Else
If a > 0 Then
Excel.Application.DisplayAlerts = False
Range(Cells(i - a, 1), Cells(i, 1)).MergeCells = True
Range(Cells(i - a, 2), Cells(i, 2)).MergeCells = True
Range(Cells(i - a, 3), Cells(i, 3)).MergeCells = True
a = 0
Excel.Application.DisplayAlerts = True
End If
End If
Next
End Sub
单列合并
Sub MergeSameCells()
Dim lRow As Integer
Application.DisplayAlerts = False
With ActiveSheet
lRow = .Range("A65536").End(xlUp).Row
For i = lRow To 2 Step -1
If .Cells(i, 1).Value = .Cells(i - 1, 1).Value Then
.Range(.Cells(i - 1, 1), .Cells(i, 1)).Merge
End If
Next
End With
Application.DisplayAlerts = True
End Sub
代码来源于互联网 |
|