Copy x rows from a worksheet and paste x rows into another worksheet n times

Solved/Closed
christol_1566 Posts 7 Registration date Thursday April 11, 2019 Status Member Last seen April 25, 2019 - Apr 11, 2019 at 06:26 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Apr 29, 2019 at 11:16 AM
Hi,
I am struggling to create a macro that can copy an excel range of data (A1:B50) from sheet 1 and paste into sheet 2 for 4000 times. Currently, sheet 2 contains a column of data with 4000 rows. Hence, i am trying to insert and paste (A1:B50) from sheet 1 into each row in sheet 2. it should visually look like this:

Sheet 1(A1:B50)
1 Cat
2 Dog
.
.
.
49 Bird

Sheet 2
Row# Name
1 Mr Tom
2 Mr Pat
.
.
.
4000 Ms Mary
The final version should be

Sheet 2
# Name
Row1 Mr Tom
1 Cat
2 Dog
.
.
.
49 Bird

Row 2 Mr Pat
1 Cat
2 Dog
.
.
.
49 Bird
.
.
.

Row 4000 Ms Mary
1 Cat
2 Dog
.
.
.
49 Bird

3 responses

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555
Apr 18, 2019 at 11:45 AM
Hi Christol,

Sheet1 Data:
1 Cat
2 Dog
3 Mouse
4 Lion

Sheet2 Data:
1 Tom
2 Pat
3 Mike
4 Hank

Running the following code:
Sub RunMe()
Dim mCount, mRow As Integer

mRow = 2

Do
    Sheets("Sheet1").Range("A1:B5").Copy
    Sheets("Sheet2").Range(Cells(mRow, "A"), Cells(mRow, "B")).Insert shift:=xlDown
    mRow = mRow + 6
    mCount = mCount + 1
Loop Until mCount = 4

End Sub


Will change the data on Sheet2 into:
1 Tom
1 Cat
2 Dog
3 Mouse
4 Lion

2 Pat
1 Cat
2 Dog
3 Mouse
4 Lion

3 Mike
1 Cat
2 Dog
3 Mouse
4 Lion

4 Hank
1 Cat
2 Dog
3 Mouse
4 Lion



Best regards,
Trowa

1
christol_1566 Posts 7 Registration date Thursday April 11, 2019 Status Member Last seen April 25, 2019
Apr 19, 2019 at 12:55 PM
Hi TrowaD,
I did use your code but instead i got the reverse result in sheet 2. Here is the code

Sub RunMe()
Dim mCount, mRow As Integer

mRow = 2

Do
Sheets("Sheet1").Range("A2:C48").Copy
Sheets("Sheet2").Range(Cells(mRow, "A"), Cells(mRow, "B")).Insert shift:=xlDown
mRow = mRow + 47
mCount = mCount + 1
Loop Until mCount = 10

End Sub

My result looks like this in sheet 2:
1 Cat
2 Dog
3 Mouse
4 Lion
.
.
.
40 Lion
pasted 10 times(1-40) Then the next row had the following below
41 Tom
42 Pat
43 Mike
44 Hank

Cat, Dog, Mouse and Lion were not pasted in between Tom, Pat, Mike and Hank. The actual data that i copied had 3 columns and 47 rows that is why i made mRow= mRow + 47. Was i wrong to do this?
0
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555
Apr 23, 2019 at 11:43 AM
Hi Christol,

My guess would be that your Sheet2 has a header. Row 2 would then be your first row of data which is pushed down. So change "mRow = 2" into "mRow = 3", if that is the case.

Your inserted data will start at row 3 and contains 47 rows. Your second row of data of Sheet2 (before you ran the code) will now be at row 50. So you want to insert at row 51. mRow was 3 and now needs to be 51 so change "mRow = mRow +47" into "mRow = mRow +48".

The amount of copied columns doesn't matter. You can even change:
"Sheets("Sheet2").Range(Cells(mRow, "A"), Cells(mRow, "B")).Insert shift:=xlDown"
into:
"Sheets("Sheet2").Cells(mRow, "A").Insert shift:=xlDown"
As Excel adjust the inserted range to match the copied range.

Do you get the correct result now?

Best regards,
Trowa
0
christol_1566 Posts 7 Registration date Thursday April 11, 2019 Status Member Last seen April 25, 2019
Apr 25, 2019 at 02:16 PM
Hi TrowaD, the code worked! Thanks. I am curious. what if i want to copy and paste 1000 times not 10 times, can i still use the same code? so Loop Until mCount = 1000
0
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555
Apr 29, 2019 at 11:16 AM
Very nice Christol and yes, you can change Loop Until mCount = 10 to 1000 if you want to copy paste a 1000 times.
0
Blocked Profile
Apr 11, 2019 at 07:04 PM
Are you familair with building for loops?

If you are, then RECORD A MACRO WITH A CUT AND PASTE OF THE DATA YOU WANT, AND LOOP IT. Then go into the macro and edit it to do what you want!

Make certian to initialize a variable for the CURRENTROW, as you will need to paste starting at a different point each time. Start with that, and we build from there!!!!


This is the easiest way to learn, because we can post some code, that you will fail to deploy, because you dont quite understand what the code does. This way! You will watch it grow, as we assist you with the changes!
0
christol_1566 Posts 7 Registration date Thursday April 11, 2019 Status Member Last seen April 25, 2019
Apr 12, 2019 at 11:55 AM
This is what it looks like when i record the macro, i am not familiar with building loops. how do i make this code into a loop?

Sub Copy2()
'
' Copy2 Macro
'

'
ActiveWindow.ScrollRow = 10
ActiveWindow.ScrollRow = 9
ActiveWindow.ScrollRow = 8
ActiveWindow.ScrollRow = 7
ActiveWindow.ScrollRow = 6
ActiveWindow.ScrollRow = 5
ActiveWindow.ScrollRow = 4
ActiveWindow.ScrollRow = 3
ActiveWindow.ScrollRow = 2
ActiveWindow.ScrollRow = 1
Range("A2:C48").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
Rows("3:3").Select
Selection.Insert Shift:=xlDown
ActiveWindow.SmallScroll Down:=-9
End Sub
0
Blocked Profile
Apr 12, 2019 at 01:17 PM
A for loop is comprised as such:

Dim thecount
Dim theendcount
Theendcount =10
For thecount = 0 to theendcount
'Do you code here
Next


The above code will perform the loop eleven times.
0
christol_1566 Posts 7 Registration date Thursday April 11, 2019 Status Member Last seen April 25, 2019
Apr 15, 2019 at 11:29 AM
This is how the code looks when i combine yours with the recorded macro. I keep getting an error 1004 saying select method of range failed. Is something missing in the code? thanks.

Sub Copy_3()
Dim thecount
Dim theendcount
theendcount = 10
For thecount = 0 To theendcount
Worksheets("Sheet2").Range("A2:C48").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
Rows("3:3").Select
Selection.Insert Shift:=xlDown
Next
End Sub
0
Blocked Profile
Apr 15, 2019 at 11:41 AM
You have to specify the activeworkbook before selecting the sheet.
0
Blocked Profile
Apr 15, 2019 at 12:10 PM
Btw, once this loop has completed once it will just diplicate it ten more times. You have keep track of the count and advance the rows by a multiple of the count
0
andreajones123 Posts 1 Registration date Friday April 12, 2019 Status Member Last seen April 12, 2019
Updated on Apr 12, 2019 at 11:19 AM
This is the best forum regarding Ms. Excel for both beginners as well as the experienced personals. If they follow this forum properly and regularly then they will be learning a lot regarding the software. Most of the logo design companies also maintain their data over Excel as it helps them in sorting and finding the relevant data quickly.
-1
Blocked Profile
Apr 12, 2019 at 11:21 AM
I disagree, with respect. Any company that is profitable, is using a database for record keeping, Not excel!
0