Macro from rows to coloumns

quincymondragon

New Member
Joined
Jun 5, 2013
Messages
5
Hi all, i've got a file excel with this scheme:

[TABLE="width: 860"]
<tbody>[TR]
[TD="width: 86"]Lat[/TD]
[TD="width: 86"]Long[/TD]
[TD="width: 86"]Lat[/TD]
[TD="width: 86"]Long[/TD]
[TD="width: 86"]Lat[/TD]
[TD="width: 86"]Long[/TD]
[TD="width: 86"]Lat[/TD]
[TD="width: 86"]Long[/TD]
[TD="width: 86"]Lat[/TD]
[TD="width: 86"]Long[/TD]
[/TR]
[TR]
[TD]Lat. #1[/TD]
[TD]Long. #1[/TD]
[TD]Lat. #2[/TD]
[TD]Long. #2[/TD]
[TD]Lat. #3[/TD]
[TD]Long. #3[/TD]
[TD]Lat. #4[/TD]
[TD]Long. #4[/TD]
[TD]Lat. #...[/TD]
[TD]Long. #...[/TD]
[/TR]
[TR]
[TD]Lat. #5[/TD]
[TD]Long. #5[/TD]
[TD]Lat. #6[/TD]
[TD]Long. #6[/TD]
[TD]Lat. #7[/TD]
[TD]Long. #7[/TD]
[TD]Lat. #8[/TD]
[TD]Long. #8[/TD]
[TD]Lat. #...[/TD]
[TD]Long. #...[/TD]
[/TR]
[TR]
[TD]Lat. #...[/TD]
[TD]Long. #...[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

i wolud like to traspose into this:

[TABLE="width: 172"]
<tbody>[TR]
[TD="width: 86"]Lat[/TD]
[TD="width: 86"]Long[/TD]
[/TR]
[TR]
[TD]Lat. #1[/TD]
[TD]Long. #1[/TD]
[/TR]
[TR]
[TD]Lat. #2[/TD]
[TD]Long. #2[/TD]
[/TR]
[TR]
[TD]Lat. #3[/TD]
[TD]Long. #3[/TD]
[/TR]
[TR]
[TD]Lat. #4[/TD]
[TD]Long. #4[/TD]
[/TR]
[TR]
[TD]Lat. #...[/TD]
[TD]Long. #...[/TD]
[/TR]
[TR]
[TD]Lat. #5[/TD]
[TD]Long. #5[/TD]
[/TR]
[TR]
[TD]Lat. #6[/TD]
[TD]Long. #6[/TD]
[/TR]
[TR]
[TD]Lat. #7[/TD]
[TD]Long. #7[/TD]
[/TR]
[TR]
[TD]Lat. #8[/TD]
[TD]Long. #8[/TD]
[/TR]
[TR]
[TD]Lat. #...[/TD]
[TD]Long. #...[/TD]
[/TR]
[TR]
[TD]Lat. #...[/TD]
[TD]Long. #...[/TD]
[/TR]
</tbody>[/TABLE]

How i could do this with a macro? Someone could help me?
Thank you!!!
 
result on sheet 2
Code:
Sub a()
LC = ActiveSheet.UsedRange.Columns.Count
LR = ActiveSheet.UsedRange.Rows.Count
drow = 2
For j = 2 To LR
  For c = 1 To LC Step 2
    Range(Cells(j, c), Cells(j, c + 1)).Copy Sheets(2).Cells(drow, "A")
    drow = drow + 1
  Next
Next
End Sub
 
Upvote 0
Ok, i've tried, it works!!
However, i modified '.Copy Sheets(2).' into '.Copy Sheet("Name Sheet").' and it overwrites data presents into the next row.
Which code i could write to insert n row as coloumn to be inserted or max coloumn used in the sheet?

Thank you again!
 
Upvote 0
So, i've adjust the code to my case, here is what i've got:

Dim drow, d, j, c, col1, col2, col3 As Integer

Sub a()
LC = 0
LR = 0
col1 = 1
col2 = 3
col3 = 5

LC = ActiveSheet.UsedRange.Columns.Count
LR = ActiveSheet.UsedRange.Rows.Count
drow = 2

For j = 2 To LR
For c = 7 To LC Step 2

If Cells(j, c) = "0" Then j = j + 1
If Cells(j, c) = "0" Then c = 7
If IsEmpty(Cells(j, c)) Then j = j + 1
If IsEmpty(Cells(j, c)) Then c = 7

Range(Cells(j, col1), Cells(j, col1 + 1)).Copy Sheets("Foglio2").Cells(drow, "A")
Range(Cells(j, col2), Cells(j, col2 + 1)).Copy Sheets("Foglio2").Cells(drow, "C")
Range(Cells(j, col3), Cells(j, col3 + 1)).Copy Sheets("Foglio2").Cells(drow, "E")

Range(Cells(j, c), Cells(j, c + 1)).Copy Sheets("Foglio2").Cells(drow, "G")
drow = drow + 1

Next
Next

End Sub


However, it loops a lot during the copy-paste of last row, i couldn't understand why. Someone could help me?!?
Thank you
 
Upvote 0

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