VBA Copy and Paste Range - Application Defined or Object Defined Error

avd88

Board Regular
Joined
Jan 18, 2016
Messages
112
Hello,

Hoping I could get some help. I'm working on a project and keep getting an application defined or object defined error.
I'm trying to select a range from A3 to column K with a dynamic row variable depending on the last row with data in it and clear contents.

Then I need to select the same range from 2 different spreadsheets and copy and paste values to the sheet where the data was cleared.

After checking the name of the spreadsheets in the code a couple of times I can't seem to find any issue with the syntax.
I believe that the issue is somewhere on the way I'm selecting a range to copy, this is where I'm getting the error:

Code:
Sheets("Materiales Riego VC").Range(Cells(3, 1), Cells(nextrowRiegoMaster, 11)).Copy

This is my full code:

VBA Code:
'MASTER

    Dim nextrowMaster As Integer
    Dim nextrowRiegoMaster As Integer
    Dim nextrowJardinesMaster As Integer
    
    nextrowMaster = WorksheetFunction.CountA(Sheets("Materiales Master").Range("D:D"))

    Sheets("Materiales Master").Range(Cells(3, 1), Cells(nextrowMaster, 11)).ClearContents
    
    nextrowRiegoMaster = WorksheetFunction.CountA(Sheets("Materiales Riego VC").Range("D:D"))
    nextrowJardinesMaster = WorksheetFunction.CountA(Sheets("Materiales Jardines VC").Range("D:D"))
    nextrowMaster = WorksheetFunction.CountA(Sheets("Materiales Master").Range("D:D"))

    Sheets("Materiales Riego VC").Range(Cells(3, 1), Cells(nextrowRiegoMaster, 11)).Copy
    Sheets("Materiales Master").Cells(nextrowMaster + 1, 1).PasteSpecial Paste:=xlPasteValues
    
    nextrowMaster = WorksheetFunction.CountA(Sheets("Materiales Master").Range("D:D"))
    
    Sheets("Materiales Jardines VC").Range(Cells(3, 1), Cells(nextrowJardinesMaster, 11)).Copy
    Sheets("Materiales Master").Cells(nextrowMaster + 1, 1).PasteSpecial Paste:=xlPasteValues
 

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).
Try...
VBA Code:
Range(Sheets("Materiales Riego VC").Cells(3, 1), Sheets("Materiales Riego VC").Cells(nextrowRiegoMaster, 11)).Copy
 
Upvote 0
You're welcome.
Just so you know if you don't qualify the sheet name to all the ranges (Cells(?, ?) is just a single cell range) then it refers to the ActiveSheet so your code was telling Excel to look at 2 different sheets "Materiales Riego VC" and the active sheet.
A range must be on the one sheet which is why it was throwing the error (if "Materiales Riego VC" was the active sheet when your code was run then it wouldn't have erred).
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
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