Columns to Rows..?

GFP1963

New Member
Joined
Jan 12, 2018
Messages
17
Given data prepared as in the attached spreadsheet

Given a sheeet with Column A of values (say names) with corresponding columns B, C, D, etc. containing values relevant to row A,
how would you created additional rows for the names in Column A, 1 each for each value in columns B, C, D, etc.

<style>table { }td { padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 12pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; }.xl63 { color: white; font-size: 10pt; font-weight: 700; font-family: Calibri; text-align: left; background: black none repeat scroll 0% 0%; }.xl64 { font-size: 10pt; font-weight: 700; font-family: Calibri; }.xl65 { font-size: 10pt; font-weight: 700; font-family: Calibri; text-align: left; }.xl66 { font-size: 10pt; font-family: Calibri; }.xl67 { color: black; font-size: 10pt; font-family: Calibri; }.xl68 { color: white; font-size: 10pt; font-weight: 700; font-family: Calibri; background: black none repeat scroll 0% 0%; }.xl69 { color: white; font-size: 10pt; font-family: Calibri; background: black none repeat scroll 0% 0%; }</style>
GIVEN THIS
NAMEE1E2e3e4e5
Sullivansullivan1sullivan2sullivan3sullivan4sullivan5
Rodríguezrodriguesz1rodriguez2rodriguez3rodriguez4rodriguez5
Burnsburns1burns1burns3burns4burns5
END UP WITH THIS
Sullivansullivan1
Sullivansullivan2
Sullivansullivan3
Sullivansullivan4
Sullivansullivan5
Rodríguezrodriguesz1
Rodríguezrodriguesz2
Rodríguezrodriguesz3
Rodríguezrodriguesz4
Rodríguezrodriguesz5
Burnsburns1
Burnsburns2
Burnsburns3
Burnsburns4
Burnsburns5

<tbody>
</tbody>
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Using VBA and assuming raw data in sheet1 and output in sheet2 (code not tested):

Code:
Private Sub Transform()

n = 1

For i = 2 to 4
   For j = 2 to 6
      If IsEmpty(Sheet1.Cells(i, j)) Then
         GoTo ContRow
      Else
         Sheet2.Cells(n, 1).Value = Sheet1.Cells(i, 1).Value
         Sheet2.Cells(n, 2).Value = Sheet1.Cells(i, j).Value
         n = n + 1
      End If
Next j
ContRow:
Next i

End Sub
 
Upvote 0
You can do it two ways, manually or using VBA code as stated in my previous reply.

Manually you can, for each row, select the range with relevant values and copy that range. Then select an output destination and paste-special, transposing the data.
That can also be achieved using formula.
See this page for a guide.

You can also use the code-solution, see file (appearently I can't find an option to attach a file). The code will count the number of column headers and rows in the input sheet and perform your request if you press CTRL+SHIFT+T.
Do note that the code does not allow for empty headers and it will continue from one row to the next row if it encounters an empty cell.
 
Upvote 0
I am familiar with the TRANSPOSE function/formula but that would not be efficient for the number of rows we expect to have.
The code solution in the sample file you provided is awesome though! Thank you!
We can work with this.

How could I modify it if there were more rows like 'name' that were to be repeated with column e1, e2, e3, etc?
For example:

<style>table { }td { padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 11pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; }.xl63 { font-size: 14pt; font-weight: 700; font-family: Calibri; }.xl64 { font-size: 14pt; font-family: Calibri; }</style>
First NameLast NameCompany NameE1E2E3E4E5
Roy SullivanABC Companysullivan1sullivan2sullivan3sullivan4sullivan5
MariaRodr’guezXYZ Companyrodriguesz1rodriguez2rodriguez3rodriguez4rodriguez5
JosephBurns123 Companyburns1burns1burns3burns4burns5

<tbody>
</tbody>

would end up with

<style>table { }td { padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 11pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; }.xl63 { font-size: 14pt; font-weight: 700; font-family: Calibri; }.xl64 { font-size: 14pt; font-family: Calibri; }</style>
First NameLast NameCompany NameE1
Roy SullivanABC Companysullivan1
Roy SullivanABC Companysullivan2
Roy SullivanABC Companysullivan3
Roy SullivanABC Companysullivan4
Roy SullivanABC Companysullivan5

<tbody>
</tbody>


I see where in the VBA editor the module is to edit it.

Thanks again!
 
Upvote 0
Hi,

You use power query for this.

Step 1: Convert your data in table form (select your data and press CTRL + T).

Step 2: Go to Data tab in ribbon and click on "From Table". Power query window will open.

Step 3: Select Column E1 to E5 and go to "Transform tab" in ribbon. Click on "Unpivot columns".

Step 4: Close and load in "Home tab" in ribbon.

A new tab will be created and your table will be like below:

First Name
Last Name
Company Name
Attribute
Value
Roy
Sullivan
ABC Company
E1
sullivan1
Roy
Sullivan
ABC Company
E2
sullivan2
Roy
Sullivan
ABC Company
E3
sullivan3
Roy
Sullivan
ABC Company
E4
sullivan4
Roy
Sullivan
ABC Company
E5
sullivan5
Maria
Rodr’guez
XYZ Company
E1
rodriguesz1
Maria
Rodr’guez
XYZ Company
E2
rodriguez2
Maria
Rodr’guez
XYZ Company
E3
rodriguez3
Maria
Rodr’guez
XYZ Company
E4
rodriguez4
Maria
Rodr’guez
XYZ Company
E5
rodriguez5
Joseph
Burns
123 Company
E1
burns1
Joseph
Burns
123 Company
E2
burns1
Joseph
Burns
123 Company
E3
burns3
Joseph
Burns
123 Company
E4
burns4
Joseph
Burns
123 Company
E5
burns5

<tbody>
</tbody>

You can also watch these steps on youtube. Search for "How to do unpivot in Power Query". Further in future if add data in your data table, just click on refresh all. Query table will be refreshed accordingly. In new tab you will find new data which you have added in your data table.

Hope it will help.

Cheers!!!!!!!
 
Last edited:
Upvote 0
Thanks again for all your assistance. I am dead in the water with the Power Query - I work on a Mac which does not have that option. I will make use of what you have provided prior and/or get someone with a PC to jump in. Thank you again.
 
Upvote 0
arunsjain - since I do not have, or have access to a version of excel with the Power Query would it be too much to ask if you could modify the version of the sheet with the VBA code solution to provide for 6 columns of information before the columns that would be transposed to rows. So:

First NameLast NameDomainCompanyExtra 1Extra 2e1e2e3e4e5e6
RaySuliivanabc.comABCn/an/asullivan1sullivan2sullivan3sullivan4sullivan5sullivan6
JoeBurnsxyz.comXYZn/an/aburns1burns2burns3burns4burns5burns6

<tbody>
</tbody>

would return

First NameLast NameDomainCompanyExtra1Extra2E
RaySullivanabc.comABCn/an/asullivan1
RaySullivanabc.comABCn/an/asullivan2
RaySullivanabc.comABCn/an/asullivan3
RaySullivanabc.comABCn/an/asullivan4
RaySullivanabc.comABCn/an/asullivan5
RaySullivanabc.comABCn/an/asullivan6
JoeBurnsxyz.comXYZn/an/aburns1
JoeBurnsxyz.comXYZn/an/aburns2
JoeBurnsxyz.comXYZn/an/aburns3
JoeBurnsxyz.comXYZn/an/aburns4
JoeBurnsxyz.comXYZn/an/aburns5
JoeBurnsxyz.comXYZn/an/aburns6

<tbody>
</tbody>

It would be greatly appreciated.
 
Upvote 0
Try this
Code:
Sub AAAAA()
Dim lr As Long, i As Long, lc As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Sheet2").Range("A1:D1").Value = Array("First Name", "Last Name", "Company Name", "E1")
    For i = 2 To lr
        lc = Cells(i, Columns.Count).End(xlToLeft).Column
            With Sheets("Sheet2").Cells(Rows.Count, 4).End(xlUp).Offset(1).Resize(lc - 3)
                .Value = Application.Transpose(Range(Cells(i, 4), Cells(i, lc)).Value)
                .Offset(, -1).Value = Cells(i, 3).Value
                .Offset(, -2).Value = Cells(i, 2).Value
                .Offset(, -3).Value = Cells(i, 1).Value
            End With
    Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

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