'1楼附件。假设前4列是有序的,如果无序手工排下序或者采用字典来重写
Option Explicit
Sub test()
Dim arr, i, j, k, m, p
arr = Range("f6:j" & Cells(Rows.Count, "f").End(xlUp).Row + 1)
For i = 1 To UBound(arr, 1) - 1
For j = i To UBound(arr, 1) - 1
For k = 1 To UBound(arr, 2) - 1
If arr(j, k) <> arr(j + 1, k) Then Exit For
Next
If k < UBound(arr, 2) Then
m = m + 1: p = UBound(arr, 2)
For k = 1 To UBound(arr, 2): arr(m, k) = arr(i, k): Next
For k = i + 1 To j: arr(m, p) = arr(m, p) & vbNewLine & arr(k, p): Next
i = j: Exit For
End If
Next j, i
With [a1]
.Resize(Rows.Count, UBound(arr, 2)).ClearContents
.Resize(m, UBound(arr, 2)) = arr
End With
End Sub |