Rows to Columns

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
234
Office Version
2019, 2016
Platform
Windows
In my Excel sheet I have multiple rows with data from the same customer. (orange)
I need to transpose this data to one row per customer, where all invoice numbers are in 1 line as shown in image (green part)

How can this be done?
 

Attachments

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
813
Office Version
365
Platform
Windows
Hi,

Do you want a formula solution or a VBA solution?

FS
 

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
234
Office Version
2019, 2016
Platform
Windows
I need a working solution, but prefer vba.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
45,172
Office Version
365
Platform
Windows
Assuming your data is sorted by customer as per your sample, try this with a copy of your data

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, r As Long, c As Long, MaxCols As Long
  
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 2)
  For i = 2 To UBound(a)
    If a(i, 1) <> a(i - 1, 1) Then
      r = r + 1
      b(r, 1) = a(i, 1)
      b(r, 2) = a(i, 2)
      c = 2
    Else
      c = c + 1
      If c > MaxCols Then
        MaxCols = c
        ReDim Preserve b(1 To UBound(b), 1 To MaxCols)
      End If
      b(r, c) = a(i, 2)
    End If
  Next i
  With Range("E1").Resize(, MaxCols)
    .Formula = "=""Invoice ""&COLUMNS($E1:E1)-1"
    .Value = .Value
    .Cells(1).ClearContents
    .Offset(1).Resize(r).Value = b
    .EntireColumn.AutoFit
  End With
End Sub
BTW ..
I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Also suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.
 

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
234
Office Version
2019, 2016
Platform
Windows
Thanks Peter,

This works perfect!

Also updated my profile with Office version and platform.

Regards,

Gerrit
 

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,681
Office Version
2019
Platform
Windows
An alternative is to use Power Query/Get and Transform found on the Data Tab. Mcode is as follows.

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Invoice", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer"}, {{"Data", each _, type table [Customer=text, Invoice=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data], "Index", 1, 1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Invoice", "Index"}, {"Custom.Invoice", "Custom.Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.Index", type text}}, "en-US")[Custom.Index]), "Custom.Index", "Custom.Invoice", List.Sum),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"1", "Invoice1"}, {"2", "Invoice2"}, {"3", "Invoice3"}})
in
    #"Renamed Columns"
Book2
ABCD
1CustomerInvoice1Invoice2Invoice3
2Customer155
3customer2565758
4customer359
5customer46061
Sheet2
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
45,172
Office Version
365
Platform
Windows
You're welcome. Thanks for the follow-up. :)

.. and for the profile update. (y)
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
5,763
@alansidman
what about this?
CustomerInvoice1Invoice2Invoice345
Customer 155
Customer 2565758
Customer 359
Customer 46061
Customer 5100120130140150


so maybe
CustomerInvoice.1Invoice.2Invoice.3Invoice.4Invoice.5
Customer 155
Customer 2565758
Customer 359
Customer 46061
Customer 5100120130140150

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Customer"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "Invoice", each [Count][Invoice]),
    Extract = Table.TransformColumns(List, {"Invoice", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    SortLen = Table.Sort(Table.AddColumn(Extract, "Length", each Text.Length([Invoice]), Int64.Type),{{"Length", Order.Descending}}),
    Split = Table.SplitColumn(SortLen, "Invoice", Splitter.SplitTextByAnyDelimiter({","}, QuoteStyle.Csv)),
    Sort = Table.Sort(Table.RemoveColumns(Split,{"Count", "Length"}),{{"Customer", Order.Ascending}})
in
    Sort
 

sandy666

Well-known Member
Joined
Oct 24, 2015
Messages
5,763
@alansidman
or with your code modified
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Customer"}, {{"Data", each _, type table [Customer=text, Invoice=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data], "Index", 1, 1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Invoice", "Index"}, {"Custom.Invoice", "Custom.Index"}),
    #"Added Prefix" = Table.TransformColumns(#"Expanded Custom", {{"Custom.Index", each "Invoice " & Text.From(_, "en-GB"), type text}}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Prefix", {{"Custom.Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Prefix", {{"Custom.Index", type text}}, "en-US")[Custom.Index]), "Custom.Index", "Custom.Invoice", List.Sum)
in
    #"Pivoted Column"
 

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,681
Office Version
2019
Platform
Windows
Thanks Sandy. Thought about the Added Prefix step after I posted, but was to lazy to go back and amend it. Nice addition. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,102,234
Messages
5,485,562
Members
407,504
Latest member
inexperiencedOne

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top