Transposing a data table

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I would like to transpose the first table into the second table. The table will always begin in Cell A1 but could have varying row and column lengths. I have been toying with VBA but cannot find an appropriate solution.

Table 1 (original)

TESTLOCATION2016A2016B2016C2017A2017B2017C2018A2018B2018C
1UK5678910111213
2USA202122232425262728

<tbody>
</tbody>

Table 2 (desired output)

TESTLOCATIONABCYear
1UK5672016
1UK89102017
1UK1112132018
2USA2021222016
2USA2324252017
2USA2627282017

<tbody>
</tbody>


Thanks.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG16Aug31
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ray [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
ray = ActiveSheet.Cells(1).CurrentRegion
ReDim nray(1 To UBound(ray, 1) * (UBound(ray, 2) - 2) / 3, 1 To 6)
c = 1
nray(1, 1) = "TEST": nray(1, 2) = "LOCATION": nray(1, 3) = "A": nray(1, 4) = "B": nray(1, 5) = "C": nray(1, 6) = "Year"
[COLOR="Navy"]For[/COLOR] n = 2 To UBound(ray)

    [COLOR="Navy"]For[/COLOR] Ac = 3 To UBound(ray, 2) [COLOR="Navy"]Step[/COLOR] 3
         c = c + 1
        nray(c, 1) = ray(n, 1)
        nray(c, 2) = ray(n, 2)
        nray(c, 3) = ray(n, Ac)
        nray(c, 4) = ray(n, Ac + 1)
        nray(c, 5) = ray(n, Ac + 2)
        nray(c, 6) = Left(ray(1, Ac + 2), 4)
    [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 6)
    .Value = nray
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
An alternative is to use Power Query. Look at the link in my signature if you are unfamiliar with this concept. Here is the Mcode
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"TEST", "LOCATION"}, "Attribute", "Value"),
    #"Split Column by Position" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByPositions({0, 4}, false), {"Attribute.1", "Attribute.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Position",{{"Attribute.1", Int64.Type}, {"Attribute.2", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute.1", "Year"}}),
    #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Attribute.2]), "Attribute.2", "Value")
in
    #"Pivoted Column"

vABCDEF
1TESTLOCATIONYearABC
21UK2016567
31UK20178910
41UK2018111213
52USA2016202122
62USA2017232425
72USA2018262728
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,407
Messages
6,119,332
Members
448,888
Latest member
Arle8907

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