Why Does My Copy/Paste Macro Copy Rows Outside Range?

andybason

Board Regular
Joined
Jan 7, 2012
Messages
217
Office Version
  1. 2016
Hello

I've put together this macro to copy and paste data to another sheet. The source data has headings in the first row, a blank second row and the data starts in the third row. The data is dynamic and the number of rows varies. An extract is shown below.

The issue is that sometimes the macro pastes the headings even though they are outside the copy range.

Does anyone know why it is doing this?

Thanks

VBA Code:
Sub Copy()

With ThisWorkbook.Worksheets("List")
 .Range("A3:L" & .Cells(Rows.Count, "A").End(xlUp).Row).Copy Destination:=ThisWorkbook.Worksheets("Repo").Range("A" & .Rows.Count).End(xlUp).Offset(1)
End With

End Sub

Book1
ABCDEFGHIJKL
1MarketIDDate/TimeMarketNameMarketStateSelectionIDSelectionNameBackLayLTPSelectionVolMarketVolVWAP
2
317182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 1. Cloneyross Poppy6.26.66.241.952399.88
417182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 2. Dunbar Patsy6.676.8141.552399.88
517182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 3. Swift Heavily182219.514.82399.88
617182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 4. Hardly Normal9.49.89.6222.52399.88
717182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 5. Swift Harass2.662.822.821879.282399.88
817182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 6. Knockmant Pearly5.45.75.899.82399.88
917182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 1. Cloneyross Poppy6.26.66.241.952399.88
1017182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 2. Dunbar Patsy6.676.8141.552399.88
1117182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 3. Swift Heavily182219.514.82399.88
1217182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 4. Hardly Normal9.49.89.6222.52399.88
1317182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 5. Swift Harass2.662.822.821879.282399.88
1417182065503/08/2020 13:10:45Swin 3rd Aug - 13:13 A9 476mOn 6. Knockmant Pearly5.45.75.899.82399.88
1517182065503/08/2020 13:10:47Swin 3rd Aug - 13:13 A9 476mOn 1. Cloneyross Poppy6.26.66.241.952453.56
1617182065503/08/2020 13:10:47Swin 3rd Aug - 13:13 A9 476mOn 2. Dunbar Patsy6.676.8142.642453.56
1717182065503/08/2020 13:10:47Swin 3rd Aug - 13:13 A9 476mOn 3. Swift Heavily192219.514.82453.56
1817182065503/08/2020 13:10:47Swin 3rd Aug - 13:13 A9 476mOn 4. Hardly Normal9.6119.6229.082453.56
1917182065503/08/2020 13:10:47Swin 3rd Aug - 13:13 A9 476mOn 5. Swift Harass2.72.82.821921.292453.56
2017182065503/08/2020 13:10:47Swin 3rd Aug - 13:13 A9 476mOn 6. Knockmant Pearly5.35.65.4103.82453.56
Store
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I'm not sure at this stage but your code refers to sheets 'List' and 'Repo' but your XL2BB data is a sheet called 'Store'. What can you tell us about all that?
 
Upvote 0
Maybe the reason is the line of code because the last line in the "Repo" sheet is taken from the "List" sheet (colored text).

With ThisWorkbook.Worksheets("List")
.Range("A3:L" & .Cells(Rows.Count, "A").End(xlUp).Row).Copy Destination:=ThisWorkbook.Worksheets("Repo").Range("A" & .Rows.Count).End(xlUp).Offset(1)
End With
Change to:
VBA Code:
With ThisWorkbook.Worksheets("List")
 .Range("A3:L" & .Cells(Rows.Count, "A").End(xlUp).Row).Copy Destination:=ThisWorkbook.Worksheets("Repo").Range("A" & ThisWorkbook.Worksheets("Repo").Rows.Count).End(xlUp).Offset(1)
End With
 
Upvote 0
Sorry for the confusion guys. I created a duplicate of the Store sheet to test to find the issue. Store is the original name. List is the new test sheet. I copied the XL2BB from the Store sheet in error. The layout in List is the same however and the issue is the same.

Apologies!
 
Upvote 0
Maybe the reason is the line of code because the last line in the "Repo" sheet is taken from the "List" sheet (colored text).
It shouldn't be that as it is only the row count that comes from 'List' (& that should be the same for all sheets in the workbook).

The only way I have found that the headings from 'List' can get copied to 'Repo' is if 'List' has no values in column A below the heading row.
 
Upvote 0
Thanks Peter. I'll see if I can ensure that data is populated immediately below the headings.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,484
Members
448,967
Latest member
visheshkotha

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