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
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.
Hi Peter_SSs,

I use this code also for other rows in my sheet.
So sometimes duplicates are possible.
Could I change a line of code to avoid duplicate results?

Regards Gerrit.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Note to Sandy. I guess we are invisible as no response from OP. :)
 
Upvote 0
By that you mean 2 or more rows with the same Customer and same Invoice?
Yes, if I have 2 rows for the same customer, with same invoice number.
There is a another column with 2 different email addresses.
Then it will report the 2 same invoices separate, instead of only once.
 
Upvote 0
Note to Sandy. I guess we are invisible as no response from OP. :)
Thanks Alan and Sandy,

I never used Power Query, so I used the solution Peter offered to me.
(already downloaded Power Query to try! ;))
 
Upvote 0
Yes, if I have 2 rows for the same customer, with same invoice number.
Will those two (or more) rows always be together like this?
Gerrit.B 2020-07-14 1.xlsm
AB
1CustomerInvoice
2Customer 155
3Customer 256
4Customer 257
5Customer 257
6Customer 258
7Customer 359
Sheet1


Or could the duplicates be apart like this?
Gerrit.B 2020-07-14 1.xlsm
AB
1CustomerInvoice
2Customer 155
3Customer 256
4Customer 257
5Customer 258
6Customer 257
7Customer 359
Sheet1
 
Upvote 0
Will those two (or more) rows always be together like this?
Gerrit.B 2020-07-14 1.xlsm
AB
1CustomerInvoice
2Customer 155
3Customer 256
4Customer 257
5Customer 257
6Customer 258
7Customer 359
Sheet1


Or could the duplicates be apart like this?
Gerrit.B 2020-07-14 1.xlsm
AB
1CustomerInvoice
2Customer 155
3Customer 256
4Customer 257
5Customer 258
6Customer 257
7Customer 359
Sheet1
I can make make sure it is always as in your first table!
 
Upvote 0
I can make make sure it is always as in your first table!
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
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,841
Members
449,051
Latest member
excelquestion515

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