Copying 3 rows of data one under the other in a stacked manner

qwerty_logic

New Member
Joined
May 31, 2016
Messages
8
Hi All,

I need to create a VBA code to execute the following task:

Current scenario -
  • In Sheet1, I have data in column Z to column AQ
  • 3 columns starting Column Z forms 1 set, i.e. Col Z, AA, AB is one set; AC, AD, AE will be set 2 and so on (total 6 sets).
  • Data in one set has to be copy pasted one under the other (except the heading in row1)
  • Final output required in Sheet2 from Cell A2

Thanks in advance !!
 
Yes, We need to copy and stack each row one under the other. The data is similar and we need to apply pivot on the final 3 columns. So no need to add separators after each set.
OK then, try this:

Code:
Sub TransposeAndStack()
' Defines variables
Dim x As Long, LastRow As Long, LastRow2 As Long, cRange As Range


' Defines LastRow as the last row of data on Sheet1 based on column Z
LastRow = Sheets("Sheet1").Cells(Rows.Count, "Z").End(xlUp).Row
' Defines LastRow2 as the first blank row of data on Sheet2 (assumes headers already exist in row 1)
LastRow2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1


' Sets check range as Z2 to the last row of Z
Set cRange = Sheets("Sheet1").Range("Z2:Z" & LastRow)


' For each cell in the check range
For Each Cell In cRange
    ' From column Z to AQ, in steps of 3
    For x = 26 To 43 Step 3
        ' Copy the current row of each set
        Sheets("Sheet1").Range(Cells(Cell.Row, x), Cells(Cell.Row, x + 2)).Copy
        ' Paste to the next blank row of Sheet2
        Sheets("Sheet2").Range("A" & LastRow).PasteSpecial xlPasteValues
        ' Increase LastRow by 1 to account for the new data
        LastRow = LastRow + 1
    ' Next set of 3 columns
    Next x
' Next cell in check range
Next Cell


End Sub
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Getting an error at step - "Copy the current row of each set" (highlighted with red text-color). I have just modified the sheetnames as per my requirement. Please suggest.

------------------------------------------------------------------------------------------------

Sub TransposeAndStack()
' Defines variables
Dim x As Long, LastRow As Long, LastRow2 As Long, cRange As Range




' Defines LastRow as the last row of data on Sheet1 based on column Z
LastRow = Sheets("Rawdata").Cells(Rows.Count, "Z").End(xlUp).Row
' Defines LastRow2 as the first blank row of data on Sheet2 (assumes headers already exist in row 1)
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1




' Sets check range as Z2 to the last row of Z
Set cRange = Sheets("Rawdata").Range("Z2:Z" & LastRow)




' For each cell in the check range
For Each Cell In cRange
' From column Z to AQ, in steps of 3
For x = 26 To 43 Step 3
' Copy the current row of each set
Sheets("Rawdata").Range(Cells(Cell.Row, x), Cells(Cell.Row, x + 2)).Copy
' Paste to the next blank row of Sheet2
Sheets("Taxonomy").Range("A" & LastRow).PasteSpecial xlPasteValues
' Increase LastRow by 1 to account for the new data
LastRow = LastRow + 1
' Next set of 3 columns
Next x
' Next cell in check range
Next Cell




End Sub
 
Upvote 0
Hmm, I have just spotted an error where I am referencing the wrong LastRow but that wouldn't give the error you are getting. You can download my test workbook with the corrected code from HERE
 
Upvote 0
I downloaded the workbook you have created. In that workbook as well, getting the same runtime error: 1004. "Application defined or object defined error".

One more thing which I noticed in output sheet -
HEADERSHEADERSHEADERS
1A1A1A
1B1B1B

<colgroup><col width="64" span="3" style="width: 48pt;"></colgroup><tbody>
</tbody>

This is the output you are pulling. whereas I want it like
HEADERSHEADERSHEADERS
1A1A1A
2A2A2A
3A3A3A
4A4A4A
5A5A5A
6A6A6A
1B1B1B
2B2B2B
3B3B3B
4B4B4B
5B5B5B
6B6B6B

<colgroup><col width="64" span="3" style="width: 48pt;"></colgroup><tbody>
</tbody>
 
Upvote 0
I see...

OK well I am not 100% sure why you would be getting the error message, the error number suggests that either there is a typo in the sheet name (when compared to the sheet name referenced in the code), or maybe the code has been placed in a worksheet module instead of a standard module. That said however I know that my own test workbook was functioning as intended so I am confused what is stopping it working at your end.

In the meantime however I can at least make sure the output is being processed in the way you want.

Leave it with me a little longer...

[EDIT]

This code (working at my end) now handles the data the way you have described above. I also added in a step which disables / re-enables screen updating to reduce flicker on screen. All that remains now is to work out why this is not working at your end...

Code:
Sub TransposeAndStack()
' Defines variables
Dim x As Long, LastRow As Long, LastRow2 As Long, cRange As Range


' Disable screen updating to reduce flicker
Application.ScreenUpdating = False


' Defines LastRow as the last row of data on Sheet1 based on column Z
LastRow = Sheets("Rawdata").Cells(Rows.Count, "Z").End(xlUp).Row
' Defines LastRow2 as the first blank row of data on Sheet2 (assumes headers already exist in row 1)
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1


' Sets check range as Z2 to the last row of Z
Set cRange = Sheets("Rawdata").Range("Z2:Z" & LastRow)


' For each cell in the check range
For Each Cell In cRange
    ' From column Z to AQ, in steps of 3
    For x = 26 To 43 Step 3
        ' Copy the current row of each set
        Sheets("Rawdata").Range(Cells(Cell.Row, x), Cells(LastRow, x + 2)).Copy
        ' Paste to the next blank row of Sheet2
        Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues
        ' Increase LastRow by 1 to account for the new data
        LastRow2 = LastRow2 + 1
    ' Next set of 3 columns
    Next x
' Next cell in check range
Next Cell


' Re-enable screen updating
Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0
Hey.. Thanks for your support @fishboy. I don't know why I am still getting the same error.

Though, I was able to do it the hard way (without using loop for the iterations). Below mentioned code is working for me. I am not using the transpose functionality here as that was not my requirement.

Hope this may help others as well !!

Code:
Sub TransposeAndStack()
' Defines variables
Dim x As Long, LastRow As Long, LastRow2 As Long


' Disable screen updating to reduce flicker
Application.ScreenUpdating = False


' Defines LastRow as the last row of data on Rawdata based on column Z
LastRow = Sheets("Rawdata").Cells(Rows.Count, "Z").End(xlUp).Row
' Defines LastRow2 as the first blank row of data on Taxonomy (assumes headers already exist in row 1)
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1


' Set-wise copy and paste data: Z2 to the last row of Z


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("Z2:AB" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AC2:AE" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AF2:AH" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AI2:AK" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AL2:AN" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AO2:AQ" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


' Re-enable screen updating
Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0
Hey.. Thanks for your support @fishboy. I don't know why I am still getting the same error.

Though, I was able to do it the hard way (without using loop for the iterations). Below mentioned code is working for me. I am not using the transpose functionality here as that was not my requirement.

Hope this may help others as well !!

Code:
Sub TransposeAndStack()
' Defines variables
Dim x As Long, LastRow As Long, LastRow2 As Long


' Disable screen updating to reduce flicker
Application.ScreenUpdating = False


' Defines LastRow as the last row of data on Rawdata based on column Z
LastRow = Sheets("Rawdata").Cells(Rows.Count, "Z").End(xlUp).Row
' Defines LastRow2 as the first blank row of data on Taxonomy (assumes headers already exist in row 1)
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1


' Set-wise copy and paste data: Z2 to the last row of Z


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("Z2:AB" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AC2:AE" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AF2:AH" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AI2:AK" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AL2:AN" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


ActiveWorkbook.Worksheets("Rawdata").Activate
ActiveWorkbook.Worksheets("Rawdata").Range("AO2:AQ" & LastRow).Select
Selection.Copy
Sheets("Taxonomy").Activate
LastRow2 = Sheets("Taxonomy").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Taxonomy").Range("A" & LastRow2).PasteSpecial xlPasteValues


' Re-enable screen updating
Application.ScreenUpdating = True


End Sub
Technically my code never actually used the transpose function within Excel, however in effect what we were doing was transposing data which is how that made its way into the sub name.

I am sorry we couldn't get to the bottom of why you were getting the error, however I am glad to hear that you now have a working macro based on snippets of my code. At least you managed to pick up enough suitable code out of it that you could write your own solution.
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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