比较通用的: sub rr MergeData (2,1,"sheet1")'对工作表“sheet1”,根据第2列数据,对第1列进行合并 end sub Sub MergeData(数据列 As Long, 合并列 As Long,处理工作表 As String) Dim i As Long, n As Long Dim xm As String Application.DisplayAlerts = False With Sheets(处理工作表) i = 2 xm = Chr(10) & .Cells(2, 合并列) & Chr(10) For n = 3 To .Range("A65536").End(xlUp).Row + 1 If .Cells(n, 数据列) <> .Cells(n - 1, 数据列) Then .Range(.Cells(i, 合并列), .Cells(n - 1, 合并列)).MergeCells = True .Cells(i, 合并列) = Mid(xm, 2, Len(xm) - 2) i = n xm = Chr(10) & .Cells(n, 合并列) & Chr(10) Else xm = IIf(Trim(.Cells(n, 合并列)) <> "" And InStr(xm, Chr(10) & .Cells(n, 合并列) & Chr(10)) = 0, xm & .Cells(n, 合并列) & Chr(10), xm) End If Next With .Range(.Cells(2, 合并列), Cells(n - 1, 合并列)) .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter End With End With Application.DisplayAlerts = True End Sub |