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 Contributor Last seen December 27, 2022 - Apr 29, 2019 at 11:16 AM
        TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Contributor Last seen December 27, 2022 - Apr 29, 2019 at 11:16 AM
        Related:         
- Copy x rows from a worksheet and paste x rows into another worksheet n times
- Transfer data from one excel worksheet to another automatically - Guide
- Add worksheet vba - Guide
- Excel vba copy worksheet to another workbook without opening ✓ - Excel Forum
- Tiktok hashtags trending copy and paste - TikTok Forum
- Insert a new sheet at the end of the tab names and paste the range names starting in cell a1. autofit columns a:b and name the worksheet as range names. ✓ - Excel Forum
3 responses
                
        
                    TrowaD
    
        
                    Posts
            
                
            2921
                
                            Registration date
            Sunday September 12, 2010
                            Status
            Contributor
                            Last seen
            December 27, 2022
            
            
                    555
    
    
                    
Apr 18, 2019 at 11:45 AM
    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:
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
 
                
                
            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
                        
                    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!
            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!
                
        
                    christol_1566
    
        
                    Posts
            
                
            7
                
                            Registration date
            Thursday April 11, 2019
                            Status
            Member
                            Last seen
            April 25, 2019
            
    
Apr 12, 2019 at 11:55 AM
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
    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
                
        
                    christol_1566
    
        
                    Posts
            
                
            7
                
                            Registration date
            Thursday April 11, 2019
                            Status
            Member
                            Last seen
            April 25, 2019
            
    
Apr 15, 2019 at 11:29 AM
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
    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
                
        
                    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
    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.
                
                
             
        
    
    
    
    
Apr 19, 2019 at 12:55 PM
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?
Apr 23, 2019 at 11:43 AM
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
Apr 25, 2019 at 02:16 PM
Apr 29, 2019 at 11:16 AM