如果你的“块”的价值观已经在顺序,它们出现在你的榜样,那么我认为这会工作:
Sub FunnySort()
Dim ws As Worksheet
Dim row, lastRow, newRow, newCol, block, oldBlock As Integer
Dim fromCell, toCell As Range
Set ws = Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
oldBlock = ws.Range("B2").Value2
newCol = 1
For row = 3 To lastRow
block = ws.Cells(row, 2).Value2
If block = oldBlock And newCol = 1 Then
' same initial block -- don't do anything
Else
If block <> oldBlock Then
newRow = 2
newCol = newCol + 5
oldBlock = block
End If
Set fromCell = ws.Cells(newRow, newCol)
Set toCell = ws.Cells(newRow, newCol + 4)
ws.Range(fromCell, toCell).Value2 = ws.Range("A" & row & ":E" & row).Value2
newRow = newRow + 1
ws.Range("A" & row & ":E" & row).Clear
End If
Next row
End Sub
如果不是为了,那是很容易的补充。另外,这假设如果你的“块”值跳过数字(即从3到5),它也不会跳过一列的数据块,它只需要下一个可用数据。从你的例子来看,我无法分辨你想要什么样的行为。如果你想跳过,那很容易融入。