Copy Range until blank row N times VBA

darthbane

New Member
Joined
Dec 13, 2017
Messages
28
Hello all,
hoping to get your assistance with a VBA script.

I am looking to copy a range from one sheet and paste it to another sheet 4 times over.

for example:
range on sheet1:
Cellvalue
A1A
A2B
A3C
A4

<tbody>
</tbody>

the VBA would only copy the range A1:A3 (leaving the blank cell) and copy it onto another sheet 4 times over and paste a specific value in the adjacent cell
example:
cellvaluevalue2
a1apaste1
a2apaste2
a3apaste3
a4apaste4
a5bpaste1
a6bpaste2
a7bpaste3
a8bpaste4
a9cpaste1
a10cpaste2
a11cpaste3

<tbody>
</tbody>

each time i run the script the initial range to be copied can change in size.

hope you can help! :)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this

Code:
Sub test_Copy_Range()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, lr As Long
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    For Each c In sh1.Range("A1", sh1.Range("A" & Rows.Count).End(xlUp))
        lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
        sh2.Range("A" & lr)(2).Resize(4).Value = c.Value
        sh2.Range("B" & lr)(2).Value = "paste1"
        sh2.Range("B" & lr)(2).AutoFill Destination:=Range("B" & lr + 1 & ":B" & lr + 4), Type:=xlFillDefault
    Next
End Sub
 
Upvote 0
Try this

Code:
Sub test_Copy_Range()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, lr As Long
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    For Each c In sh1.Range("A1", sh1.Range("A" & Rows.Count).End(xlUp))
        lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
        sh2.Range("A" & lr)(2).Resize(4).Value = c.Value
        sh2.Range("B" & lr)(2).Value = "paste1"
        sh2.Range("B" & lr)(2).AutoFill Destination:=Range("B" & lr + 1 & ":B" & lr + 4), Type:=xlFillDefault
    Next
End Sub


it is giving me a subscript out of range error when i try to run it..
 
Upvote 0
it is giving me a subscript out of range error when i try to run it..

You have to put the names of your origin-destination leaves in this part:

Code:
    Set sh1 = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")   'Origin
    Set sh2 = Sheets("[COLOR=#ff0000]Sheet2[/COLOR]")   'Destination
 
Upvote 0
You have to put the names of your origin-destination leaves in this part:

Code:
    Set sh1 = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")   'Origin
    Set sh2 = Sheets("[COLOR=#ff0000]Sheet2[/COLOR]")   'Destination

oh wow, how did i not realize! thanks.

however i have one more ask - the values that get pasted into sheet two start in the 2nd row. i would actually like the paste to being in the 5th row. is there a way to do this with the script given?
 
Upvote 0
Try:

Code:
Sub test_Copy_Range()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim c As Range, lr As Long
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    For Each c In sh1.Range("A1", sh1.Range("A" & Rows.Count).End(xlUp))
        lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
        If lr < 5 then lr = 5
        sh2.Range("A" & lr)(2).Resize(4).Value = c.Value
        sh2.Range("B" & lr)(2).Value = "paste1"
        sh2.Range("B" & lr)(2).AutoFill Destination:=Range("B" & lr + 1 & ":B" & lr + 4), Type:=xlFillDefault
    Next
End Sub
 
Upvote 0
works perfectly when i try it! however when i try to use it to paste into my destination sheet that has a dynamic table, it will start pasting under the table. do you know if there is anyway to fix this or will i have to convert the table to a normal range?
 
Upvote 0
The macro always looks for the last row with data, but with a table, even if the cells are empty, the last row with data is the last row of the table.
You would help the macro a lot if you convert your table into normal range.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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