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
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
According to your attachment a VBA demonstration as a beginner starter (data must be sorted on column B) :​
VBA Code:
Sub Demo1()
        Dim Rg(1) As Range, R&
        Set Rg(1) = [B1]:   R = 1
        [D1].CurrentRegion.Offset(1).Clear
        Application.ScreenUpdating = False
    With [A1].CurrentRegion.Columns(2)
      Do
            Set Rg(0) = Rg(1)(2)
            Set Rg(1) = .Find(Rg(0).Text, , , 1, , 2)
            R = R + 1
            Cells(R, 4).Value2 = Rg(0).Value2
        With Range(Rg(0), Rg(1))
            Cells(R, 5).Resize(, .Rows.Count).Value2 = Application.Transpose(.Columns(0))
        End With
      Loop Until Rg(1).Row = .Rows.Count
    End With
        Application.ScreenUpdating = True
        Erase Rg
End Sub
 
Upvote 0
Thanks for the reply Marc. It works great with the sample data, but I should probably have mentioned that my real data set is approx 600k rows. So when I run that VBA on it, I just eventually crash Excel. Anyhting else I can try, or am I just going to need to find a way to reduce my data-set?
 
Upvote 0
Hi & welcome to MrExcel.
Will the orders in col B always be grouped together?
 
Upvote 0
Ok, how about
VBA Code:
Sub SBest()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long
   
   Ary = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
   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
 
Upvote 0
Ok, how about
VBA Code:
Sub SBest()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long
  
   Ary = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
   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
I get an immediate "Run-time error '7': Out of Memory"
 
Upvote 0
Do you only have two columns of data on the sheet?
 
Upvote 0
Do you only have two columns of data on the sheet?
Yes. This is a sample of the real data, just in case that is useful

Book1
ABCDEFGHIJKLMNOPQR
1ItemOrder
2BJT053-1316440563
3BJT107-1316440563
4BJT113-1316440563
5BJT104-1316440563
6BJT102-1316440563
7BJT110-1316440563
8BJT104-2316440563
9BJT104-3316440563
10BJT107-2316440563
11BJT101-1316440563
12BJT100-1316440563
13BJT108346279439
14BJT109346279439
15BJT174346279439
16BJT161346279439
17BJT104-3346279439
18BJT104346279439
19BJT110346279439
20BJT105346279439
21BJT118346279439
22BJT192346279439
23BJT052346279439
24BJT057346279439
25BJT016346279439
26BJT038346279439
27BJT103346279439
28BJT182346279439
29BJT189346279439
30BJT193346279439
31BJT237346279439
32BJT250346279439
33BJT273346279439
34BJT306346279439
35BJT308346279439
36BJT451346279439
37BJT465346279439
38BJT480346279439
39BR109346279439
40T0203346286089
41ST155346286102
42T0148346286102
43T0217346286102
44T0219346286102
45T0068346286102
46T0023346286161
47T0095346286161
48T0111346286161
49GTSUBB1032346286161
50T0223346286161
51T0224346286161
52GTRBH11155346286161
53T0050346286242
54T0011346286242
55GTSUBB1032346286242
56T0215346286242
57T0222346286242
58SCFBE80346286242
59T0148346286294
60T0210346286294
61T0217346286294
62T0219346286294
63T0068346286294
64T0226346286294
65T0201346286353
66T0152346286366
67GTSUBB1032346286366
68T0224346286366
69GTRBH11155346286366
70T0410346286366
71T0201346286414
72GTAIRR1026346286427
73T0221346286427
74GTHELB1060346286427
75T0057346286454
76T0212346286454
77T0213346286454
78T0214346286454
79T0219346286454
80T0068346286454
81T0168346286454
82T0227346286454
83T0226346286454
84T0145346286454
85T0159346286454
86T0403346286454
87T0303346286454
88GTAIRR1026346286566
89RB20073346286566
90T0151346286566
91T0221346286566
92GTAIRR1026346286607
93GTAIRB1027346286607
94RB20073346286607
95T0151346286607
96T0211346286607
97T0221346286607
98GTWTCG1111346286607
99RB20034B346286607
100T0508346286607
101GTAIRR1026346286687
Sheet1
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,877
Members
449,130
Latest member
lolasmith

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