可以用两种方式达成效果,一种是VBA宏模式。一种数据透视表
VBA宏模式需先对表进行排序,然后循环判断需要合并的单元格范围,合并单元格即可。
数据透视表可以直接得出,但不能合并单元格,可以清空相同内容。
宏代码如下,我以排序第一列和第二列为标准
Sub hb()
Dim i%
rowlong = [a50000].End(xlUp).Row
ActiveSheet.Sort.SortFields.Add Key:=Range(Cells(2, 1), Cells(rowlong, 1)), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range(Cells(2, 2), Cells(rowlong, 2)), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet6").Sort
.SetRange Range(Cells(1, 1), Cells(rowlong, 100))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For i = 2 To rowlong
If Cells(i, 1) = Cells(i + 1, 1) And Cells(i - 1, 1) <> Cells(i, 1) Then toprow = i
If Cells(i, 1) <> Cells(i + 1, 1) And i <> toprow Then
Range(Cells(toprow + 1, 1), Cells(i, 1)) = ""
Range(Cells(toprow, 1), Cells(i, 1)).Merge
End If
Next
rowlong = [b50000].End(xlUp).Row
For i = 2 To rowlong
If Cells(i, 2) = Cells(i + 1, 2) And Cells(i - 1, 2) <> Cells(i, 2) Then toprow = i
If Cells(i, 2) <> Cells(i + 1, 2) And i <> toprow Then
Range(Cells(toprow + 1, 2), Cells(i, 2)) = ""
Range(Cells(toprow, 2), Cells(i, 2)).Merge
End If
Next
End Sub