Object Required Error

rguy84

Board Regular
Joined
May 18, 2011
Messages
112
I am getting an error on the code below, and not sure why. Can somebody shed some light? (The macro isn't complete btw)
Code:
Sub SortAndRemoveDupes()
    Dim lastCell, lastContact As Long
    Dim rListSort, rOldList As Range
    Dim strRowSource As String
 
    Set lastCell = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    [COLOR=red]Set lastContact = Sheets("Contact").Range("H" & Row.Count).End(xlUp).Row[/COLOR]
 
    Sheets("Sheet2").Range("A1:E" & lastCell).Clear
 
    Set rOldList = Sheets("Contact").Range("D2:H" & lastContact)
 
    'copy unique list to sheet2
    rOldList.AdvancedFilter Action:=xlFilterCopy, _
               CopyToRange:=Sheet2.Cells(1, 1), Unique:=True
    'Set range variable to the new non dupe list
    Set rListSort = Sheet2.Range("A1", Sheet1.Range("E").End(xlUp))
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You need to Dim your variables correctly and remove Set from the lines that get row numbers

Code:
Sub SortAndRemoveDupes()
    Dim lastCell As Long, lastContact As Long
    Dim rListSort As Range, rOldList As Range
    Dim strRowSource As String
 
    lastCell = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    lastContact = Sheets("Contact").Range("H" & Row.Count).End(xlUp).Row
 
    Sheets("Sheet2").Range("A1:E" & lastCell).Clear
 
    Set rOldList = Sheets("Contact").Range("D2:H" & lastContact)
 
    'copy unique list to sheet2
    rOldList.AdvancedFilter Action:=xlFilterCopy, _
               CopyToRange:=Sheet2.Cells(1, 1), Unique:=True
    'Set range variable to the new non dupe list
    Set rListSort = Sheet2.Range("A1", Sheet1.Range("E").End(xlUp))
End Sub
 
Upvote 0
You have declared it as long so the statement shall be:
Code:
[COLOR=black]lastContact = Sheets("Contact").Range("H" & Row[COLOR=red]s[/COLOR].Count).End(xlUp).Row[/COLOR]
And it should be Rows.Count as you have used correctly in the previous statement.

When you use "Set", VBA expects an object reference to be set which is not applicable in current case.
 
Upvote 0
Peter, I used set because after removing them, I get error 424 on lastContact = ...

I also thought I could do Dim var1, var2 as Range and it applies to both
 
Last edited:
Upvote 0
You only use Set with Object variables.

This should fix it

Rich (BB code):
lastContact = Sheets("Contact").Range("H" & Rows.Count).End(xlUp).Row
 
Upvote 0
You can use Sheet1 provided that the sheet's code name is actually Sheet1.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
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