Option Explicit
Sub test()
Dim i, j, k, arr, n
With Sheets("数据库原表")
arr = .Range("a2:e" & .Cells(Rows.Count, "c").End(xlUp).Row + 1)
End With
ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2))
For i = 1 To UBound(arr, 1) - 1
For j = i To UBound(arr, 1) - 1
If Len(arr(j + 1, 2)) > 0 Or Len(arr(j + 1, 3)) = 0 Then
For k = i To j
n = n + 1
If k = i Then
brr(n, 1) = arr(k, 5): brr(n, 2) = arr(k, 3): brr(n, 3) = arr(k, 4)
If j > i Then
brr(n, 4) = arr(k + 1, 3): brr(n, 5) = arr(k + 1, 4)
Else
Exit For
End If
k = k + 1
Else
brr(n, 4) = arr(k, 3): brr(n, 5) = arr(k, 4)
End If
Next
i = j: Exit For
End If
Next j, i
With Sheets("转换后生成的表").[a3]
.Resize(Rows.Count - 2, UBound(arr, 2)).ClearContents
.Resize(n, UBound(brr, 2)) = brr
End With
End Sub |