Convert a column of data to a header for another column

AlexiaK

New Member
Joined
Mar 14, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello, I have some data in one column and the questions that represent these data in another column. In column A the questions keep repeating, so I do not want to keep everything; but I do want to keep the answers since they represent a different participant each time. Is there a way to convert the data of a column to a header for another column?
Here is an example of what I mean:

This is what I have
What is your gender?Female
What is your age?19
What is your preferred type of exercise?Aerobic
What is your gender?Male
What is your age?21
What is your preferred type of exerciseAnaerobic

This is what I would like
What is your gender?What is your age?What is your preferred type of exercise?
Female19Aerobic
Male21Anaerobic
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
How many questions are there? Will it always be 3?
 
Upvote 0
This macro assumes you have headers in row 1 and your data starts in row 2. The result will be displayed in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, x As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS
        .UsedRange.ClearContents
        .Range("A1").Resize(, 22).Value = WorksheetFunction.Transpose(srcWS.Range("A2:A23"))
    End With
    For x = 2 To LastRow Step 22
        With desWS
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 22).Value = WorksheetFunction.Transpose(srcWS.Range("B" & x).Resize(22))
        End With
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This macro assumes you have headers in row 1 and your data starts in row 2. The result will be displayed in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, x As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS
        .UsedRange.ClearContents
        .Range("A1").Resize(, 22).Value = WorksheetFunction.Transpose(srcWS.Range("A2:A23"))
    End With
    For x = 2 To LastRow Step 22
        With desWS
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 22).Value = WorksheetFunction.Transpose(srcWS.Range("B" & x).Resize(22))
        End With
    Next x
    Application.ScreenUpdating = True
End Sub

Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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