Copy + paste data range using loop on paste + X spaces

Imbetteratjava

New Member
Joined
Jan 6, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

New to some of the VB terminology in excel and I can't seem to get this to work.
I've taken some examples on this site and put them together with others but I'm coming up short on a solid solution.

I have data in a range of B2:G2 down to blank rows (undefined)
I want to copy the first row of data (B2:G2) and paste it in another sheet starting at point (A2) then + ~about 10 to 20 spaces to give me room for a large picture
Then copy the next set eg (B3:G3)

Here's what I've been messing around with so far.

VBA Code:
Sub NewMacro()
Dim ws1, ws2 As Worksheet
Dim copyRng, cell As Range
Dim lCopyLastRow As Long

Set ws1 = ThisWorkbook.Sheets(1)
Set ws2 = ThisWorkbook.Sheets(2)

'Find last used row in the copy range based on data in column A
lCopyLastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    

Set copyRng = ws1.Range("B2:G" & lCopyLastRow).End(x1Down).Row    'Getting an application-defined or object-defined error

For Each cell In copyRng
    ws2.Range("A" & 2 + 10 * copyRng.Rows(cell.Row).Row - 30).Value = cell.Value
Next cell


End Sub

Any tips or help appreciated.
Thanks.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi & welcome to MrExcel.
You have x1Down but it should be xlDown (lower case L not number 1)
 
Upvote 0
Hi & welcome to MrExcel.
You have x1Down but it should be xlDown (lower case L not number 1)
That might've been an unfortunate accident when I copied it over, my code is showing that xldown not x1. I'll fix that.
 
Upvote 0
Ok, try it like
VBA Code:
Sub NewMacro()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyRng As Range, Rw As Range
Dim lCopyLastRow As Long, i As Long

Set ws1 = ThisWorkbook.Sheets(1)
Set ws2 = ThisWorkbook.Sheets(2)

'Find last used row in the copy range based on data in column A
lCopyLastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    

Set copyRng = ws1.Range("B2:G" & lCopyLastRow)    'Getting an application-defined or object-defined error
i = 2
For Each Rw In copyRng.Rows
   ws2.Range("A" & i).Resize(, 7).Value = Rw.Value
   i = i + 20
Next Rw


End Sub
 
Upvote 0
Solution
Ok, try it like
VBA Code:
Sub NewMacro()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyRng As Range, Rw As Range
Dim lCopyLastRow As Long, i As Long

Set ws1 = ThisWorkbook.Sheets(1)
Set ws2 = ThisWorkbook.Sheets(2)

'Find last used row in the copy range based on data in column A
lCopyLastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
   

Set copyRng = ws1.Range("B2:G" & lCopyLastRow)    'Getting an application-defined or object-defined error
i = 2
For Each Rw In copyRng.Rows
   ws2.Range("A" & i).Resize(, 7).Value = Rw.Value
   i = i + 20
Next Rw


End Sub
This worked liked a charm, thank you for the help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

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