Data Transformation: Transpose Rows into Columns

bstaaa

New Member
Joined
Dec 29, 2017
Messages
7
I'm looking to transpose a spreadsheet that consists of a column with an ID for the whole row of data, followed by data fields listed in a typical column/row setup, similar to the table below. And for background, my particular data set has thousands of rows and about 20 or so columns.

IDNameField 1Field 2
1AX345
2BY123

<tbody>
</tbody>

However, due to a limitation of the system this data will be used in, I need to alter this structure to an alternate format where I list out the ID, the field name, followed by the answer for that field, similar to the following:


IDFieldAnswer
1NameA
1Field 1X
1Field 2345
2NameB
2Field 1Y
2Field 2123

<tbody>
</tbody>


So to solve this transformation via VBA I'm thinking I need to do the following:
  • transpose the field listing into column 2
  • have it fill ID 1 out in front of the transposed field names
  • transpose the field data into column 3
  • then have it repeat for each subsequent number

Is this the right approach? Any thoughts, ideas, or code examples are greatly appreciated.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I think this will do the trick. Assuming that the data is contained in a named range called "Data".

Code:
Public Sub transpose()
    Dim target As Worksheet
    Dim sourceData As Range
    Dim thisDataRow As Long
    Dim thisDataCol As Long
    Dim nrOfRows As Long
    Dim nrOfCols As Long
    Dim thisTargetRow As Long
    
    
    Set sourceData = Range("Data")
    nrOfRows = sourceData.Rows.Count
    nrOfCols = sourceData.Columns.Count
    
    Set target = Worksheets("Sheet2")
    
    thisTargetRow = 1
    With target
        .Cells(thisTargetRow, 1) = "ID"
        .Cells(thisTargetRow, 2) = "Field"
        .Cells(thisTargetRow, 3) = "Answer"
        For thisDataRow = 2 To nrOfRows
            For thisDataCol = 2 To nrOfCols
                thisTargetRow = thisTargetRow + 1
                .Cells(thisTargetRow, 1) = sourceData(thisDataRow, 1)
                .Cells(thisTargetRow, 2) = sourceData(1, thisDataCol)
                .Cells(thisTargetRow, 3) = sourceData(thisDataRow, thisDataCol)
            Next
        Next
    End With
End Sub

Hope this helps.

Cheers.
 
Upvote 0
Another option
Code:
Sub CopyTranspose()

   Dim Hdrs As Variant
   Dim Rws As Long
   Dim DataWs As Worksheet
   Dim Cl As Range
   Dim NxtRw As Long
   
   Set DataWs = Sheets("[COLOR=#ff0000]Data[/COLOR]")
   Hdrs = DataWs.Range("B1", DataWs.Cells(1, Columns.Count).End(xlToLeft)).Value
   Rws = UBound(Hdrs, 2)
   With Sheets("[COLOR=#ff0000]new[/COLOR]")
      .Range("A1:C1").Value = Array("ID", "Field", "Answer")
      For Each Cl In DataWs.Range("A2", DataWs.Range("A" & Rows.Count).End(xlUp))
         NxtRw = .Range("A" & Rows.Count).End(xlUp).Offset(1).Row
         .Range("A" & NxtRw).Resize(Rws).Value = Cl.Value
         .Range("B" & NxtRw).Resize(Rws).Value = Application.Transpose(Hdrs)
         .Range("C" & NxtRw).Resize(Rws).Value = Application.Transpose(Cl.Offset(, 1).Resize(, Rws).Value)
      Next Cl
   End With
End Sub
Changing sheet names in red to suit.
This assumes your header is in row 1, with data starting in A2
 
Upvote 0
Not sure which of us you're talking to, but glad we could help.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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