Copy range in Sheet B n times for each row in Sheet A

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
This should be a simple thing, but I still manage to mess it up.

In a workbook there are two sheets, A and B.
In sheet A there are 1000 rows and 7 columns. (the numbers of rows will vary.) Col A will always be filled with data (no empty cells before last row)
In sheet B there is a range, A1 to G1 to last row. Last row will vary.
What I need to do to copy the complete range in sheet B, and copy it in after each row in sheet A.

I am using this code, but something is wrong. In the example the range in sheet B has 30 rows.



VBA Code:
Sub CopyPasteRangeN_Times()
    
     Worksheets("A").Activate
     Application.ScreenUpdating = False
     Application.Calculation = xlManual
    
Dim LastrowA As Long, LastrowB As Long, i As Long

LastrowA = Range("A" & Rows.Count).End(xlUp).Row

Worksheets("B").Activate

Sheets("Sheet B").Range("A1:G" & Cells(Rows.Count, "A").End(xlUp).Row).Copy
Worksheets("A").Activate

For i = 39 To Lastrow Step 39

    Range("A" & i).Paste
    
Next i
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Which is the first row the data should be pasted into?
Also do you just want values?
 
Upvote 0
Hi,

The first row will always be row 2, and it is only values.
 
Upvote 0
So you want the data from sheet B to be pasted inserted into every other row on sheet A starting at row 2?
 
Upvote 0
This will insert the data from columns A:G on sheet B in between each row of sheet A.
VBA Code:
Sub CopyPasteRangeN_Times()
Dim rngDst As Range
Dim rngSrc As Range
  
    Application.ScreenUpdating = False
    Application.Calculation = xlManual

    Set rngDst = Worksheets("A").Range("A3")

    Set rngSrc = Sheets("B").Range("A1:G" & Sheets("B").Cells(Rows.Count, "A").End(xlUp).Row)

    Do
        rngSrc.Copy
        rngDst.Insert xlShiftDown
        Set rngDst = rngDst.Offset(1)
    Loop Until rngDst.Value = ""
    
End Sub
 
Upvote 0
Another option that does values only, as requested.
VBA Code:
Sub Brutusar()
   Dim Ary As Variant
   Dim i As Long
   
   With Sheets("B")
      Ary = .Range("A1:G" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   With Sheets("A")
      For i = .Range("A" & Rows.Count).End(xlUp).Row To 2 Step -2
         .Rows(i + 1).Resize(UBound(Ary) - 1).Insert
         .Range("A" & i).Resize(UBound(Ary), 7).Value = Ary
      Next i
   End With
End Sub
 
Upvote 0
Both codes are working perfectly. Thank you very much for sharing your knowledge and taking the time to do so!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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