Convert Table to Flat List (maybe macro automate? happy for function ideas)

Mingalator

Board Regular
Joined
Oct 28, 2008
Messages
53
Hi Folks,

I am trying to easily and quickly convert a table with 139 column / headings and variable number of rows, typically ~400, into a flat list of say 3 x 55,600 (139 x 400)

So the headings would be my unique key (project number) then another 138 headings of various categories, say period 1-138
ProjectP1P2P3P4P5P6
00001123234456567678789
00002111222333444555666

To turn into:

ProjectHeadingValue
00001P1123
00001P2234
00001P3456
00001P4567
00001P5678
00001P6789
00002P1111
00002P2222

Any ideas or pointers would be appreciated, is a macro the way to go?? Or is there a function in Excel that would achieve this?

Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Which version of Office do you have? If you have access to Power Query/Get and Transform, that's just a simple unpivot query.
 
Upvote 0
Without super testing:

VBA Code:
Option Explicit

Sub reLayout()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim lastR1 As Integer
Dim lastR2 As Integer
Dim cell As Range
Dim i As Integer

Set sh1 = Sheet1   'original sheet
Set sh2 = Sheet2   'destination sheet - headers should be already there

With sh1
    lastR1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For Each cell In sh1.Range("A2:A" & lastR1)
    For i = 1 To 137
        With sh2
            lastR2 = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            .Cells(lastR2, 1).Value = cell.Value
            .Cells(lastR2, 2).Value = sh1.Cells(1, i + 1).Value
            .Cells(lastR2, 3).Value = sh1.Cells(cell.Row, i + 1).Value
        End With
    Next i
Next cell
End Sub
 
Upvote 0
I've tested this a bit!!

' Change these two lines.
Set Ws = Worksheets("Project") ' Source of data.
Set WsDestination = Worksheets("ProjectTransformed") ' Destination of data.


VBA Code:
Public Sub subTransformData()
Dim Ws As Worksheet
Dim WsDestination As Worksheet
Dim intRow As Integer
Dim intColumns As Integer
Dim intRows As Integer
Dim rng As Range

On Error GoTo Err_Handler

    ActiveWorkbook.Save

    ' Change these two lines.
    Set Ws = Worksheets("Project") ' Source of data.
    Set WsDestination = Worksheets("ProjectTransformed") '  Destination of data.
    
    WsDestination.Cells.ClearContents
    WsDestination.Range("A1:C1").Value = Array("Project", "Heading", "Value")
        
    intColumns = Ws.Range("A1").CurrentRegion.Columns.Count - 1
    intRows = Ws.Range("A1").CurrentRegion.Rows.Count
    intRow = 2
    
    For Each rng In Ws.Range("A2").Resize(intRows - 1, 1).Cells
        With WsDestination
            .Cells(intRow, 1).Resize(intColumns, 1).Value = rng.Value
            .Cells(intRow, 2).Resize(intColumns, 1).Value = Application.WorksheetFunction.Transpose(Ws.Range("B1").Resize(1, intColumns))
            .Cells(intRow, 3).Resize(intColumns, 1).Value = Application.WorksheetFunction.Transpose(rng.Offset(0, 1).Resize(1, intColumns))
        End With
        intRow = intRow + intColumns
    Next rng
              
Exit_Handler:

    Exit Sub

Err_Handler:

    MsgBox "There has been an error, the data may not have been transformed correcty.", vbInformation, "Warning!"

    Resume Exit_Handler

End Sub
 
Upvote 0
Without super testing:

VBA Code:
Option Explicit

Sub reLayout()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim lastR1 As Integer
Dim lastR2 As Integer
Dim cell As Range
Dim i As Integer

Set sh1 = Sheet1   'original sheet
Set sh2 = Sheet2   'destination sheet - headers should be already there

With sh1
    lastR1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For Each cell In sh1.Range("A2:A" & lastR1)
    For i = 1 To 137
        With sh2
            lastR2 = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            .Cells(lastR2, 1).Value = cell.Value
            .Cells(lastR2, 2).Value = sh1.Cells(1, i + 1).Value
            .Cells(lastR2, 3).Value = sh1.Cells(cell.Row, i + 1).Value
        End With
    Next i
Next cell
End Sub
Thank you for the reply, using RoryA's suggestion of the Power Query Unpivot has solved it for me, thanks for the help though.
 
Upvote 0
I've tested this a bit!!

' Change these two lines.
Set Ws = Worksheets("Project") ' Source of data.
Set WsDestination = Worksheets("ProjectTransformed") ' Destination of data.


VBA Code:
Public Sub subTransformData()
Dim Ws As Worksheet
Dim WsDestination As Worksheet
Dim intRow As Integer
Dim intColumns As Integer
Dim intRows As Integer
Dim rng As Range

On Error GoTo Err_Handler

    ActiveWorkbook.Save

    ' Change these two lines.
    Set Ws = Worksheets("Project") ' Source of data.
    Set WsDestination = Worksheets("ProjectTransformed") '  Destination of data.
   
    WsDestination.Cells.ClearContents
    WsDestination.Range("A1:C1").Value = Array("Project", "Heading", "Value")
       
    intColumns = Ws.Range("A1").CurrentRegion.Columns.Count - 1
    intRows = Ws.Range("A1").CurrentRegion.Rows.Count
    intRow = 2
   
    For Each rng In Ws.Range("A2").Resize(intRows - 1, 1).Cells
        With WsDestination
            .Cells(intRow, 1).Resize(intColumns, 1).Value = rng.Value
            .Cells(intRow, 2).Resize(intColumns, 1).Value = Application.WorksheetFunction.Transpose(Ws.Range("B1").Resize(1, intColumns))
            .Cells(intRow, 3).Resize(intColumns, 1).Value = Application.WorksheetFunction.Transpose(rng.Offset(0, 1).Resize(1, intColumns))
        End With
        intRow = intRow + intColumns
    Next rng
             
Exit_Handler:

    Exit Sub

Err_Handler:

    MsgBox "There has been an error, the data may not have been transformed correcty.", vbInformation, "Warning!"

    Resume Exit_Handler

End Sub
Thank you for the reply, using RoryA's suggestion of the Power Query Unpivot has solved it for me, thanks for the help though.
 
Upvote 0
Just looked this up and this is a really handy function, many thanks
Glad we could help. You might want to update your profile here to show you have 365 as it will help people assisting you to know what functions you have available to you.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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