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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
83,031
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
You have x1Down but it should be xlDown (lower case L not number 1)
 
Upvote 0

Imbetteratjava

New Member
Joined
Jan 6, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
83,031
Office Version
  1. 365
Platform
  1. Windows
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

Imbetteratjava

New Member
Joined
Jan 6, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
83,031
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,190,766
Messages
5,982,817
Members
439,798
Latest member
tangojuliet

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
Top