
Originally Posted by
jenz_skallemose
The data will always be a fixed number of rows which is 45, so that particular problem will not happen.
Sub test()
Dim dic As Object, ws As Worksheet, r As Range, i As Long, ii As Long
Dim w() As Double, maxRow As Long, n As Long, e, mySheets
Const RowLimit = 45, startRow As Long = 4, endRow As Long = 10
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = 1
mySheets = VBA.Array("Ark1", "Ark2")
For i = 0 To UBound(mySheets)
With Sheets(mySheets(i)).Cells(1).CurrentRegion.Resize(RowLimit)
For Each r In .Rows(1).Cells
If Not dic.exists(r.Value) Then
ReDim w(1 To endRow - startRow + 1 + n)
Else
w = dic(r.Value)
ReDim Preserve w(1 To endRow - startRow + 1 + n)
End If
For ii = startRow To endRow
w(n + ii - startRow + 1) = r(ii).Value
Next
dic(r.Value) = w
maxRow = Application.Max(maxRow, UBound(w))
Next
n = n + endRow - startRow + 1
End With
Next
For Each e In dic
w = dic(e)
If UBound(w) < maxRow Then ReDim Preserve w(1 To maxRow)
dic(e) = w
Next
With Sheets("Ark3")
.UsedRange.Clear
.Cells(1).Resize(, dic.Count).Value = dic.keys
.Cells(2, 1).Resize(maxRow, dic.Count).Value = _
Application.Transpose(dic.items)
End With
End Sub
Bookmarks