Excel : Copy multiple columns to rows based on one column

raviansal

New Member
Joined
Mar 6, 2022
Messages
5
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
I'm looking for the most efficient approach to transferring information from three columns to one row, right next to each other depending on the company name. Consider the following example:

  1. I have a 226k data list with product details for several customers (around 30-40k).
  2. One customer can have 2 to 50 products, each with a serial number and a product number. Check out the source data pic.
Output required: One row per customer will have all the product data, serial number, and product number next to each other.

Source data
Product nameSerial numberProduct numberCustomer Name
yangoo​
12345​
55​
Panasonic​
iwg​
234567​
jhx​
Panasonic​
wzfgsxh​
uwgzd​
234567​
Panasonic​
zqwfdxutqfx​
234567​
hgv​
Panasonic​
awertz​
hwdchg​
23456​
Panasonic​
yangoo​
qixgqz​
22​
Apple​
iwg​
1234567​
3456​
Apple​
wzfgsxh​
2321​
7u47545​
Apple​
zqwfdxutqfx​
823828-33​
44​
Apple​
awertz​
4378-34​
432​
Apple​
yangoo​
84746-1i6​
214​
Apple​
iwg​
U18H3032​
124321​
Apple​
wzfgsxh​
276876​
1324​
Apple​



Desired output:
1646596383078.png
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
In Power Query:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Customer Name"}, {{"All", each List.Combine(Table.ToRows(Table.RemoveColumns(_, {"Customer Name"})))}}),
    tbl = Table.AddColumn(Group, "Count", each List.Count([All])/3),
    max = List.Max(tbl[Count]),
    tcn = List.Accumulate({1..max}, {"Customer Name"}, (s,c)=> let n = Number.ToText(c) in s & {"Product name " & n, "Serial number " & n, "Product number " & n}),
    tbl1 = Table.AddColumn(tbl, "List", each {[Customer Name]} & [All] & List.Repeat({null,null,null}, max - [Count])),
    Result = Table.FromRows(tbl1[List], tcn)
in
    Result

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Product nameSerial numberProduct numberCustomer NameCustomer NameProduct name 1Serial number 1Product number 1Product name 2Serial number 2Product number 2Product name 3Serial number 3Product number 3Product name 4Serial number 4Product number 4Product name 5Serial number 5Product number 5Product name 6Serial number 6Product number 6Product name 7Serial number 7Product number 7Product name 8Serial number 8Product number 8
2yangoo1234555PanasonicPanasonicyangoo1234555iwg234567jhxwzfgsxhuwgzd234567zqwfdxutqfx234567hgvawertzhwdchg23456
3iwg234567jhxPanasonicAppleyangooqixgqz22iwg12345673456wzfgsxh23217u47545zqwfdxutqfx823828-3344awertz4378-34432yangoo84746-1i6214iwgU18H3032124321wzfgsxh2768761324
4wzfgsxhuwgzd234567Panasonic
5zqwfdxutqfx234567hgvPanasonic
6awertzhwdchg23456Panasonic
7yangooqixgqz22Apple
8iwg12345673456Apple
9wzfgsxh23217u47545Apple
10zqwfdxutqfx823828-3344Apple
11awertz4378-34432Apple
12yangoo84746-1i6214Apple
13iwgU18H3032124321Apple
14wzfgsxh2768761324Apple
15
Sheet1
 
Upvote 0
Welcome to the MrExcel board!

Power Query may well be your best bet but if you want to try a macro approach, you could try this.
I have assumed that the raw data is grouped/sorted by Customer Name like you sample data.

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, ProdCount As Long, uba As Long, x As Long, y As Long
  Dim Cust As String
 
  a = Range("A1", Range("D" & Rows.Count).End(xlUp).Offset(1)).Value
  uba = UBound(a)
  ReDim b(1 To uba, 1 To 1)
  b(1, 1) = "Customer Name"
  i = 2
  k = 1
  Do
    k = k + 1
    Cust = a(i, 4)
    j = 0
    y = 1
    b(k, 1) = a(i, 4)
    Do While a(i + j, 4) = Cust And i < UBound(a)
      j = j + 1
      If j > ProdCount Then
        ProdCount = ProdCount + 1
        ReDim Preserve b(1 To uba, 1 To ProdCount * 3 + 1)
        b(1, ProdCount * 3 - 1) = "Product name " & ProdCount
        b(1, ProdCount * 3) = "Serial number " & ProdCount
        b(1, ProdCount * 3 + 1) = "Product number " & ProdCount
      End If
      For x = 1 To 3
        y = y + 1
        b(k, y) = a(i + j - 1, x)
      Next x
    Loop
    i = i + j
  Loop Until i >= uba
  With Range("F1").Resize(k, UBound(b, 2))
    .Value = b
    .Columns.AutoFit
  End With
End Sub

My sample data and results:

raviansal.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1Product nameSerial numberProduct numberCustomer NameCustomer NameProduct name 1Serial number 1Product number 1Product name 2Serial number 2Product number 2Product name 3Serial number 3Product number 3Product name 4Serial number 4Product number 4Product name 5Serial number 5Product number 5Product name 6Serial number 6Product number 6Product name 7Serial number 7Product number 7Product name 8Serial number 8Product number 8
2yangoo1234555PanasonicPanasonicyangoo1234555iwg234567jhxwzfgsxhuwgzd234567zqwfdxutqfx234567hgvawertzhwdchg23456
3iwg234567jhxPanasonicAppleyangooqixgqz22iwg12345673456wzfgsxh23217u47545zqwfdxutqfx823828-3344awertz4378-34432yangoo84746-1i6214iwgU18H3032124321wzfgsxh2768761324
4wzfgsxhuwgzd234567Panasonic
5zqwfdxutqfx234567hgvPanasonic
6awertzhwdchg23456Panasonic
7yangooqixgqz22Apple
8iwg12345673456Apple
9wzfgsxh23217u47545Apple
10zqwfdxutqfx823828-3344Apple
11awertz4378-34432Apple
12yangoo84746-1i6214Apple
13iwgU18H3032124321Apple
14wzfgsxh2768761324Apple
Sheet1
 
Upvote 0
Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,755
Messages
6,126,683
Members
449,328
Latest member
easperhe29

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