Macro Error 1004: Application-defined or object-defined error

boldcode

Active Member
Joined
Mar 12, 2010
Messages
347
Hello,

I have a macro that is suppose to copy cells with data from one worksheet and paste them to another except when I run the Macro I get the Macro Error 1004. Here is my code below:

Code:
Sub UpdateReport()
    Dim lngFirstRowToAdd As Long, lngLastRowToAdd As Long, lngLastReportRow As Long, lngLastColumnToAdd As Long
    lngFirstRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).End(xlUp).Offset(1, 0).Row
    lngLastRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).Row
    lngLastColumnToAdd = Sheets("rptNoTune").Range("A10").End(xlToRight).Column
    lngLastReportRow = Sheets("rptTune").Range("A65536").End(xlUp).Offset(1, 0).Row
    Sheets("rptNoTune").Range(Cells(lngFirstRowToAdd, 1), Cells(lngLastRowToAdd, lngLastColumnToAdd)).Copy
    Sheets("rptTune").Range("A" & lngLastReportRow).PasteSpecial
    Application.CutCopyMode = False
End Sub

The Macro stops on this particular line of code:

Code:
Sheets("rptNoTune").Range(Cells(lngFirstRowToAdd, 1), Cells(lngLastRowToAdd, lngLastColumnToAdd)).Copy
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
When using the syntax
Range(Cells(a,b), Cells(y,z))

And you specify the Sheet on the range, you must also specify the sheet on the Cells as well..

Sheets("...").Range(Sheets("...").Cells(a,b), Sheets("...").Cells(y,z))

Use a With structure to make it simpler without naming the sheet 3 times..

Code:
With Sheets("...")
    .Range(.Cells(a,b), .Cells(y,z)).Copy
End With

Hope that helps.
 
Upvote 0
jonmo1,

I changed the code to what you suggested and it works. I have a question for you though how can I streamline my these parts of my code a little better.

In this part how can I change Range("A65536") to last row in worksheet

Code:
  lngFirstRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).End(xlUp).Offset(1, 0).Row
    lngLastRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).Row
In this part how can I change Range("A10") which is the start row of my pivot table to start at pivot field titles and again change Range("A65536") to last row in worksheet:

Code:
lngLastColumnToAdd = Sheets("rptNoTune").Range("A10").End(xlToRight).Column
    lngLastReportRow = Sheets("rptTune").Range("A65536").End(xlUp).Offset(1, 0).Row
 
Upvote 0
Cells(Rows.Count,"A") is preferred over Range("A65536")

Rows.Count = available rows
And will reflect the correct # regardless of XL Version.
 
Upvote 0
Jonmo1,

I just want to throw another "Thank You" your way. In the past, I have always been successful creating Pivot Tables using:
Code:
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim PRange As Range

...

    Set PRange = WSR.Cells(1, 1).Resize(FinalRow, 9)
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
            xlDatabase, SourceData:=PRange.Address)

    Set PT = PTCache.CreatePivotTable(TableDestination:=WSR. _
        Cells(2, 2), TableName:="PivotByEmp")
However, for the first time, I was trying to create the PivotTable on a sheet other than the one that contained the source data and I kept getting the 1004 error.

What finally worked (after reading your response in this thread) was to change the SourceData to include the sheet as:
Code:
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
            xlDatabase, SourceData:=WSR.Cells(1, 1).Resize(FinalRow, 9))
   
    Set PT = PTCache.CreatePivotTable(TableDestination:=WSPE. _
        Cells(2, 2), TableName:="PivotByEmp")
Thanks for your help!
 
Upvote 0
Hi Jonm01,

need help , i have below code facing same error . can you please check and let me know solution

Sub Filter_Excel_Dates(Date1 As String, Date2 As String)
Dim LastRow As Long




LastRow = ThisWorkbook.Worksheets("Potential CCRA SME Customer").Range("V" & Rows.Count, "V").End(xlUp).Row


ThisWorkbook.Worksheets("Potential CCRA SME Customer").Range("V1:V10000" & LastRow).AutoFilter Field:=1, Operator:= _
xlFilterValues, Criteria2:=Array(1, Date1, 1, Date2)
 
Upvote 0
Welcome to the forum. :)

This line:

Code:
LastRow = ThisWorkbook.Worksheets("Potential CCRA SME Customer").Range("V" & Rows.Count, "V").End(xlUp).Row

should read:

Code:
LastRow = ThisWorkbook.Worksheets("Potential CCRA SME Customer").Range("V" & Rows.Count).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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