I don't understand why I'm getting this error

ouadad

Board Regular
Joined
Jun 19, 2005
Messages
213
I'm just matching two cells and then copying and pasting from one worksheet to another, the copy line gives me an "Application Defined or Object Defined Error".

Code:
Sub Add_Forecast_Data_to_Demand_Data()
Dim totalrows1 As Long, totalrows2 As Long, iRow1 As Long, iRow2 As Long, part1 As String, part2 As String
Dim loc1 As String, loc2 As String
totalrows1 = Sheets("Parts_Locs_Merged_Rows").UsedRange.Rows.Count
totalrows2 = Sheets("Forecast_June").UsedRange.Rows.Count
For iRow2 = 2 To 10
    part2 = Sheets("Forecast_June").Cells(iRow2, 1)
    loc2 = Sheets("Forecast_June").Cells(iRow2, 2)
    For iRow1 = 2 To totalrows1
        part1 = Sheets("Parts_Locs_Merged_Rows").Cells(iRow1, 1)
        loc1 = Sheets("Parts_Locs_Merged_Rows").Cells(iRow1, 2)
        If (part1 = part2 And loc1 = loc2) Then
            Sheets("Forecast_June").Range(Cells(iRow2, 3), Cells(iRow2, 15)).Copy
            Sheets("Parts_Locs_Merged_Rows").Cells(iRow1, 15).PasteSpecial xlPasteValues
            Exit For
        End If
    Next iRow1
Next iRow2
End Sub
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Is the error on this line?
Sheets("Forecast_June").Range(Cells(iRow2, 3), Cells(iRow2, 15)).Copy

When you use the syntax Range(Cells(), Cells())
And you specify the Sheet on the Range, you must also specify the sheet on both Cells.

Try changing that to
Sheets("Forecast_June").Range(Sheets("Forecast_June").Cells(iRow2, 3), Sheets("Forecast_June").Cells(iRow2, 15)).Copy
 
Upvote 0
You're welcome..

FYI, it happens because of certain assumptions VBA makes in your code.

When you use Range or Cells, if you do NOT specify the sheet it refers to, then VBA assumes it should refer to whatever the currently active sheet is.

So if the sheet Forcast_June is NOT the active sheet at the time this runs (Say Sheet1 is active) then VBA reads it like this
Sheets("Forecast_June").Range(Sheets("Sheet1").Cells(iRow2, 3), Sheets("Sheet1").Cells(iRow2, 15)).Copy
Which makes no sense at all, because Sheet1 is not contained within Forcase_June
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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