Converting List of Values to Records

SBest

New Member
Joined
Sep 7, 2021
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
I have a large list of data - order numbers and items. There could be anything from 1 to 200 items per order. I am trying to convert this so that each order number is a single row, with relevant items listed horizontally. So, each order number would be a record with between 1 and 200 items listed against it. I feel like there is a simple soution that I'm missing, but I can't find it and am struggling to put it in the right wording to find the answer online. I have uploaded a simplified example - my data as it is in columns A:B - I am trying to get to something like D:H

Book3
ABCDEFGH
1ItemOrderOrder
2X11XYZ
3Y12ABCX
4Z1
5A2
6B2
7C2
8X2
9
Sheet1
 
With that sort of data, I wouldn't expect any problems. Try this instead.
VBA Code:
Sub SBest()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long
  
   With Sheets("Sheet1")
      Ary = .Range("A1:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 300)
  
   For r = 2 To UBound(Ary)
      If Ary(r, 2) <> Ary(r - 1, 2) Then
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 2)
         Nary(nr, 2) = Ary(r, 1)
         nc = 3
      Else
         nc = nc + 1
         Nary(nr, nc) = Ary(r, 1)
      End If
   Next r
   Sheets("Sheet1").Range("D2").Resize(nr, 300).Value = Nary
End Sub
That works fine with the sample of 100 rows, but get that same run-time error when trying on the full data-set
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I've just duplicated your data over 660,000 rows & don't get an Out of Memory Error.
I can only think that somewhere in the data there are some very long strings.
 
Upvote 0
Try it like this
VBA Code:
Sub SBest()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long, i As Long
   
   With Sheets("Sheet1")
      For i = 300000 To 600000
         If .Cells(i, 2) <> .Cells(i + 1, 2) Then Exit For
      Next i
      Ary = .Range("A1:B" & i).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 300)
   
   For r = 2 To UBound(Ary)
      If Ary(r, 2) <> Ary(r - 1, 2) Then
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 2)
         Nary(nr, 2) = Ary(r, 1)
         nc = 3
      Else
         nc = nc + 1
         Nary(nr, nc) = Ary(r, 1)
      End If
   Next r
   Sheets("Sheet1").Range("D2").Resize(nr, 300).Value = Nary
   With Sheets("Sheet1")
      Ary = .Range("A" & i + 1 & ":B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 300)
   
   For r = 2 To UBound(Ary)
      If Ary(r, 2) <> Ary(r - 1, 2) Then
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 2)
         Nary(nr, 2) = Ary(r, 1)
         nc = 3
      Else
         nc = nc + 1
         Nary(nr, nc) = Ary(r, 1)
      End If
   Next r
   Sheets("Sheet1").Range("D" & Rows.Count).End(xlUp).Offset(1).Resize(nr, 300).Value = Nary

End Sub
 
Upvote 0
Solution
Try it like this
VBA Code:
Sub SBest()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long, i As Long
  
   With Sheets("Sheet1")
      For i = 300000 To 600000
         If .Cells(i, 2) <> .Cells(i + 1, 2) Then Exit For
      Next i
      Ary = .Range("A1:B" & i).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 300)
  
   For r = 2 To UBound(Ary)
      If Ary(r, 2) <> Ary(r - 1, 2) Then
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 2)
         Nary(nr, 2) = Ary(r, 1)
         nc = 3
      Else
         nc = nc + 1
         Nary(nr, nc) = Ary(r, 1)
      End If
   Next r
   Sheets("Sheet1").Range("D2").Resize(nr, 300).Value = Nary
   With Sheets("Sheet1")
      Ary = .Range("A" & i + 1 & ":B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 300)
  
   For r = 2 To UBound(Ary)
      If Ary(r, 2) <> Ary(r - 1, 2) Then
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 2)
         Nary(nr, 2) = Ary(r, 1)
         nc = 3
      Else
         nc = nc + 1
         Nary(nr, nc) = Ary(r, 1)
      End If
   Next r
   Sheets("Sheet1").Range("D" & Rows.Count).End(xlUp).Offset(1).Resize(nr, 300).Value = Nary

End Sub
That has done the job - many thanks for your help!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top