Rows to Columns

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
237
Office Version
  1. 2019
  2. 2016
Platform
  1. 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

  • 2020-07-14_095253.png
    2020-07-14_095253.png
    10.1 KB · Views: 44

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
In that case the modification is fairly simple. Try this version.

VBA Code:
Sub Rearrange_v2()
  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
      If a(i, 2) <> a(i - 1, 2) Then
        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
    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

Gerrit.B 2020-07-14 1.xlsm
ABCDEFGH
1CustomerInvoiceInvoice 1Invoice 2Invoice 3
2Customer 155Customer 155
3Customer 256Customer 2565758
4Customer 257Customer 359
5Customer 257Customer 46061
6Customer 258
7Customer 359
8Customer 460
9Customer 461
Sheet1
VBA Code:
Sub Rearrange2() 'Email
 Dim a As Variant, b As Variant, x As Variant
  Dim i As Long, r As Long, c As Long, MaxCols As Long
    ThisWorkbook.Sheets("RemindersByExcel1").Activate
  a = Range("A1", Range("G" & Rows.Count).End(xlUp)).Value
  x = 3 'change number to get data from correct column.
  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, x)
      c = 2
    Else
      If a(i, x) <> a(i - 1, x) Then
        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, x)
      End If
    End If
  Next i
  ThisWorkbook.Sheets("Mail").Activate
  With Range("A11").Resize(, MaxCols)
    .Formula = "=""Email ""&COLUMNS($E1:E1)-1"
    .Value = .Value
    .Cells(1).ClearContents
    .Offset(1).Resize(r).Value = b
    .EntireColumn.AutoFit
  End With
End Sub

I change your code a little to rearrange all values.
Then I noticed I have the data also as you mentioned in your first table.
 
Upvote 0
just for fun as alternative
CustomerInvoiceCustomerInvoice.1Invoice.2Invoice.3Invoice.4Invoice.5
Customer 155Customer 155
Customer 256Customer 2565758
Customer 257Customer 359
Customer 258Customer 46061
Customer 359Customer 5100120130140150
Customer 460
Customer 460
Customer 461
Customer 5100
Customer 5120
Customer 5130
Customer 5140
Customer 5150
Customer 5100
Customer 155
Customer 257

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 List.Distinct([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
 
Upvote 0
So, are you all sorted now?
Most of the columns are ok, but only where te values are as in your fist table are mention twice.
In te columns with it's the same customer, same invoice, but with 2 rows as there are 2 contacts.
The output will be Contact1, Contact2, Contact1, Contact2.
 
Upvote 0
Most of the columns are ok, but only where te values are as in your fist table are mention twice.
In te columns with it's the same customer, same invoice, but with 2 rows as there are 2 contacts.
The output will be Contact1, Contact2, Contact1, Contact2.
I am not familiar with your data so that is pretty much meaningless to me. Up until now we have had 'Customer' and 'Invoice'. Now you are referring to 'Contact's as well.
Can you post a small set of sample dummy data and the expected results with XL2BB and explain clearly in relation to that new sample data?
 
Upvote 0
Book1
ABCDEFGHIJK
1CustomerInvoiceContactCustomerContact1Contact2Contact3Contact4Contact5
2Customer 1551Customer 11
3Customer 2562Customer 22323
4Customer 2563Customer 44
5Customer 2572
6Customer 2573
7Customer 4604
Sheet1


See results above.
 
Upvote 0
Hi @Gerrit.B,
That represents a great change. If you no longer need the invoice in the output, then try the following macro.
The data in Sheet1 the results in the sheet "Mail"

VBA Code:
Sub RowsToColumns()
  Dim a As Variant, dic1 As Object, i As Long
  
  Application.DisplayAlerts = False
  Set dic1 = CreateObject("Scripting.Dictionary")
  a = Sheets("Sheet1").Range("A2:G" & Sheets("Sheet1").Range("A" & Rows.Count).End(3).Row).Value
  For i = 1 To UBound(a, 1)
    dic1(a(i, 1)) = dic1(a(i, 1)) & a(i, 3) & "|"
  Next
  With Sheets("Mail").Range("A11")
    .Resize(dic1.Count, 2).Value = Application.Transpose(Array(dic1.keys, dic1.items))
    .Offset(, 1).Resize(dic1.Count).TextToColumns .Offset(, 1), xlDelimited, Other:=True, OtherChar:="|"
  End With
End Sub
 
Upvote 0
Hi @Gerrit.B,
That represents a great change. If you no longer need the invoice in the output, then try the following macro.
The data in Sheet1 the results in the sheet "Mail"

VBA Code:
Sub RowsToColumns()
  Dim a As Variant, dic1 As Object, i As Long
 
  Application.DisplayAlerts = False
  Set dic1 = CreateObject("Scripting.Dictionary")
  a = Sheets("Sheet1").Range("A2:G" & Sheets("Sheet1").Range("A" & Rows.Count).End(3).Row).Value
  For i = 1 To UBound(a, 1)
    dic1(a(i, 1)) = dic1(a(i, 1)) & a(i, 3) & "|"
  Next
  With Sheets("Mail").Range("A11")
    .Resize(dic1.Count, 2).Value = Application.Transpose(Array(dic1.keys, dic1.items))
    .Offset(, 1).Resize(dic1.Count).TextToColumns .Offset(, 1), xlDelimited, Other:=True, OtherChar:="|"
  End With
End Sub
Thanks Dante,

I prefer to use code from Peter as i have to run this code 6 times to get all the data in the correct format.
After every run I go to the next column to collect my data.

Regards,

Gerrit
 
Upvote 0
I prefer to use code from Peter as i have to run this code 6 times to get all the data in the correct format.
After every run I go to the next column to collect my data.

My code does what you asked for in post #28.
Did you at least try it?
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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