Swapping rows to colums in excel

ashmanyeah

New Member
Joined
Mar 20, 2018
Messages
4
Hi all,

I have downloaded a dataset and it is not consistent (in terms of how it displays the data) compared with other datasets I have. Does anyone know the most efficient way I could transform the data to look like the dataset on the right hand side? For example, I would want the years in a single column with the next column being the values for the variable in question. Obviously the country names would need to be replicated for each year, so if my dataset was from 1960 to 2010, Aruba would show up 60 times, then followed by Afghanistan etc.

If I have not explained myself sufficiently, please let me know.

Thanks

THIS IS THE IMAGE - I could not upload it to this thread for some reason (maybe too large of a filesize?)

https://postimg.org/image/h1fz6tl57/
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi & welcome to the board.
How about
Code:
Sub copyTrans()

   Dim Cl As Range
   Dim Hdr As Range
   Dim r As Long
   Dim ws As Worksheet
   
Application.ScreenUpdating = False
   Set ws = Sheets("pcode")
   Set Hdr = ws.Range("B1", ws.Cells(1, Columns.Count).End(xlToLeft))
   r = Hdr.Count
   For Each Cl In ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp))
      With Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)
         .Offset(1, 1).Resize(r).Value = Application.Transpose(Hdr.Value)
         .Offset(1, 2).Resize(r).Value = Application.Transpose(Cl.Offset(, 1).Resize(, r).Value)
         Cl.Copy .Offset(1).Resize(r)
      End With
   Next Cl
End Sub
 
Upvote 0
Hi & welcome to the board.
How about
Code:
Sub copyTrans()

   Dim Cl As Range
   Dim Hdr As Range
   Dim r As Long
   Dim ws As Worksheet
   
Application.ScreenUpdating = False
   Set ws = Sheets("pcode")
   Set Hdr = ws.Range("B1", ws.Cells(1, Columns.Count).End(xlToLeft))
   r = Hdr.Count
   For Each Cl In ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp))
      With Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)
         .Offset(1, 1).Resize(r).Value = Application.Transpose(Hdr.Value)
         .Offset(1, 2).Resize(r).Value = Application.Transpose(Cl.Offset(, 1).Resize(, r).Value)
         Cl.Copy .Offset(1).Resize(r)
      End With
   Next Cl
End Sub

Hi there Fluff,

thanks for the swift response. Forgive me but I am unsure what I would do with that code. Is there a help page dedicated to inputting lines of code in Excel?

Kind Regards,

Ash
 
Upvote 0
You need to change pcode to the name of the sheet containing your data.
You'll also need to change this line
Code:
With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("A" & Rows.Count).End(xlUp)
to reflect the name of the sheet you want the data copied to.
 
Upvote 0
You need to change pcode to the name of the sheet containing your data.
You'll also need to change this line
Code:
With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("A" & Rows.Count).End(xlUp)
to reflect the name of the sheet you want the data copied to.

That worked a treat. Thank you kindly!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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