Re: consolidate rows into single row
What is the reason to place blank row in row 1?
It's not a good practice and also a bad procedure name "sort"....
Code
Sub MOformat()
'combines in and out times onto single row for format and reporting
Dim a, i As Long, ii As Long, w, n As Long
With Cells(1).CurrentRegion
a = .Value: ReDim w(1 To UBound(a, 2))
mySort a, 3, UBound(a, 1), 4, 0
With CreateObject("Scripting.Dictionary")
For i = 3 To UBound(a, 1)
If a(i, 4) <> "" Then
If Not .exists(a(i, 1)) Then
ReDim w(1 To UBound(a, 2), 1 To 1)
Else
w = .Item(a(i, 1))
ReDim Preserve w(1 To UBound(a, 2), 1 To UBound(w, 2) + 1)
End If
For ii = 1 To UBound(a, 2)
w(ii, UBound(w, 2)) = a(i, ii)
Next
.Item(a(i, 1)) = w
ElseIf a(i, 5) <> "" Then
If .exists(a(i, 1)) Then
w = .Item(a(i, 1))
For ii = 1 To UBound(w, 2)
If w(5, ii) = "" Then w(5, ii) = a(i, 5): Exit For
Next
.Item(a(i, 1)) = w
End If
End If
Next
w = .items
End With
With .Offset(, .Columns.Count + 1)
.Rows(1).Value = a: n = 3
For i = 0 To UBound(w)
.Rows(n).Resize(UBound(w(i), 2)).Value = Application.Transpose(w(i))
n = n + UBound(w(i), 2)
Next
With .Offset(2).Resize(n)
.sort .Cells(1), 1, , , , , , 1
End With
End With
End With
End Sub
Display More