Transposing multiple columns to rows based on unique values from first column

mattc1985

New Member
Joined
Jul 4, 2023
Messages
3
Office Version
  1. 2013
Dear all,

I need to transpose Columns B:N based on unique values in Column A to rows. I have already found a VBA script on this forum that acomplishes this to a degree:

It was posted in this thread: Transpose Unique Values in column to row
by user DanteAmor

VBA Code:
Sub TransposeColumns()
  Dim a As Variant, b() As Variant
  Dim dic As Object, i As Long, lin As Long, col As Long, n As Long
  Dim lr As Long
  
  lr = ActiveSheet.Range("A:C").Find("*", , xlValues, , xlByRows, xlPrevious).Row
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2:C" & lr).Value2
  Range("D2", Cells(Rows.Count, Columns.Count)).ClearContents
  
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + 1
    If dic(a(i, 1)) > n Then n = dic(a(i, 1))
  Next
  n = (n * 2) + 1
  ReDim b(1 To dic.Count, 1 To n)
  dic.RemoveAll
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      lin = lin + 1
      col = 1
      b(lin, col) = a(i, 1)
    Else
      lin = Split(dic(a(i, 1)), "|")(0)
      col = Split(dic(a(i, 1)), "|")(1) + 2
    End If
    dic(a(i, 1)) = lin & "|" & col
    b(lin, col + 1) = a(i, 2)
    b(lin, col + 2) = a(i, 3)
  Next
  Application.ScreenUpdating = False
  Range("E2").Resize(dic.Count, n).Value = b
  Application.ScreenUpdating = True
End Sub

I would appreciate if someone would give me some guidance how to expand this script where you have more than three columns. Thank you. Your help is greatly appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi @mattc1985
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

You could put a sample of your data (it can be generic data).
And how do you want the result?​
To give examples use XL2BB tool

Look at the example of this post, the poster put examples of how the data is in his sheets and put 2 examples of the results he wants.
He was so precise in his examples that the macro worked first time.​

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Upvote 0
Thank you very much for your prompt response. I followed your advice and install the plug in. Here are the minisheets:

Source:

CbC_report_wo faultcodes_SoW-Slovenia (2).xlsm
ABCDEFGHIJ
1Doc numberNameAddressTypeDate of acquisitionDate of inspectionDate of final reportArt. numberEANPrice
24408Joel BlackAbbey road 10, LondonMAC5IONIC 5762 22321.03.202325.05.202306.06.202344411110
34408Joel BlackAbbey road 10, LondonMAC5IONIC 5762 22421.03.202325.05.202306.06.20235556667
44408Joel BlackAbbey road 10, LondonMAC5IONIC 5762 22521.03.202325.05.202306.06.20231011223255421020123335091,42
54435Samantha WhiteTottenham street 126, LondonULTIMATEYTH 3788 14917.08.202225.05.202319.06.20234441116
64435Samantha WhiteTottenham street 126, LondonULTIMATEYTH 3788 15017.08.202225.05.202319.06.202381738770421020124525013,58
74443Eugene PortoCourt road 55, YorkP11 5666 225517.12.202225.05.202308.06.202344411110
84443Eugene PortoCourt road 55, YorkP11 5666 225617.12.202225.05.202308.06.20238172184413,95
94589KOSTIANTYN LUKASHENKOLeicester square 189, DoverPRO88THE 3652 12605.10.202231.05.202308.06.202344411120
104589KOSTIANTYN LUKASHENKOLeicester square 189, DoverPRO88THE 3652 12705.10.202231.05.202308.06.20235556668
114589KOSTIANTYN LUKASHENKOLeicester square 189, DoverPRO88THE 3652 12805.10.202231.05.202308.06.20238174564621,58
124598BIGBANG INVEST LLCSummer palace circle 85D, SwanagePILIPY9 5123 104028.05.202131.05.202308.06.20234441115
134598BIGBANG INVEST LLCSummer palace circle 85D, SwanagePILIPY9 5123 104128.05.202131.05.202308.06.20235556664
144598BIGBANG INVEST LLCSummer palace circle 85D, SwanagePILIPY9 5123 104228.05.202131.05.202308.06.2023844772063,32
154598BIGBANG INVEST LLCSummer palace circle 85D, SwanagePILIPY9 5123 104328.05.202131.05.202308.06.2023811235546,24
Source


Result:

CbC_report_wo faultcodes_SoW-Slovenia (2).xlsm
ABCDEFGHIJKLMNOPQRS
1Doc numberNameAddressTypeDate of acquisitionDate of inspectionDate of final reportArt. numberEANPriceArt. numberEANPriceArt. numberEANPriceArt. numberEANPrice
24408Joel BlackAbbey road 10, LondonMAC5IONIC 5762 22321.03.202325.05.202306.06.20234441111055566671011223255421020123335091,42
34435Samantha WhiteTottenham street 126, LondonULTIMATEYTH 3788 14917.08.202225.05.202319.06.2023444111681738770421020124525013,58
44443Eugene PortoCourt road 55, YorkP11 5666 225517.12.202225.05.202308.06.2023444111108172184413,95
54589KOSTIANTYN LUKASHENKOLeicester square 189, DoverPRO88THE 3652 12605.10.202231.05.202308.06.20234441112055566688174564621,58
64598BIGBANG INVEST LLCSummer palace circle 85D, SwanagePILIPY9 5123 104028.05.202131.05.202308.06.202344411155556664844772063,32811235546,24
Results


So, basically I would like to transpose H,I,J columns in Source to columns H,I,J,K,L,M,N,O,PQ,R,S in Results based on unique number in column A in Source sheet. I hope I managed to explain it properly :) Thank you for your input.
 
Upvote 0
Thanks for the example, now it is very clear to me.

Try the following macro:

VBA Code:
Sub Transposing_multiple_columns()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim i&, j&, k&, y&, nMax&, nRow&, nCol&
 
  Set sh1 = Sheets("Source")
  Set sh2 = Sheets("Results")
  Set dic = CreateObject("Scripting.Dictionary")
 
  a = sh1.Range("A1:J" & sh1.Range("A" & Rows.Count).End(3).Row).Value
  For i = 2 To UBound(a, 1)
    dic(a(i, 1)) = dic(a(i, 1)) + 1
    If dic(a(i, 1)) > nMax Then nMax = dic(a(i, 1))
  Next
  ReDim b(1 To dic.Count + 1, 1 To 7 + (nMax * 3))
  dic.RemoveAll
 
  For j = 1 To 7
    b(1, j) = a(1, j)
  Next
  k = 8
  For i = 1 To nMax
    b(1, k) = a(1, 8)
    b(1, k + 1) = a(1, 9)
    b(1, k + 2) = a(1, 10)
    k = k + 3
  Next
  y = 1
  For i = 2 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then
      y = y + 1
      dic(a(i, 1)) = y & "|" & 8
      For j = 1 To 7
        b(y, j) = a(i, j)
      Next
    End If
    nRow = Split(dic(a(i, 1)), "|")(0)
    nCol = Split(dic(a(i, 1)), "|")(1)
    b(nRow, nCol) = a(i, 8)
    b(nRow, nCol + 1) = a(i, 9)
    b(nRow, nCol + 2) = a(i, 10)
    dic(a(i, 1)) = nRow & "|" & nCol + 3
  Next
 
  sh2.Cells.ClearContents
  sh2.Range("A1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 1
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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