I have multiple reports that are pulling the same data. I’m trying to flag these duplicates by copying an identifier at the end of the first iteration of that data. Currently my code reads as follows:
Set OppID = OwnerName.Offset(0, -3)
Set CurrentWS = Worksheets(OwnerName.Value)
Set OppIDField = CurrentWS.Range("C1", Range("C1").End(xlDown))
IDCount = WorksheetFunction.CountIf(OppIDField, OppID)
If IDCount > 0 Then
Set Existing = OppIDField.Find(OppID)
Debug.Print Existing.Address
OwnerName.Offset(0, -5).Copy Destination:=Worksheets(OwnerName.Value).Range(Existing.Address).End(xlRight).Offset(0, 1)
Running the code as-is gives me a runtime error in the Copy section.
If I change "xlRight" to "xlUp" it will copy the correct data into the wrong place, but it at least won’t experience the runtime error and will complete the macro.
The offset is to put it into the next cell over from the last one with data. Is there a reason why xlRight would cause an error that xlUp wouldn’t?
>Solution :
The XlDirection (the argument of End method) value must be one of the following: xlDown, xlToLeft, xlToRight, or xlUp. End(xlRight) causes the runtime error (1004).
Microsoft documentation:
If the variable Existing refers to the last used cell in the row, End(xlToRight) will reach the last column in that row. Using Offset(0, 1) will exceed the sheet limit, which causes the runtime error (1004) too.
Microsoft documentation:
Try locating the destination cell using End(xlToLeft). If this doesn’t match your expected output, please share the desired cell location.
With Worksheets(OwnerName.Value)
OwnerName.Offset(0, -5).Copy Destination:=.Cells(Existing.Row, .Columns.Count).End(xlToLeft).Offset(0, 1)
End With