'Object Required' error in macro for finding duplicate using ListObjects

AskMyDog

New Member
Joined
Nov 13, 2015
Messages
16
Hi,

I am trying to write a macro which highlights the second, and all following duplicates, in a table using ListObjects

Unfortunately, it keeps returning an 'Object Required' error when arriving at the "countif" function. In the debug mode, the variable "Dupl_Cell" has a value when I hover over it, so maybe it's the variable "Name_Col"?

Here's the code:
Code:
Sub Highlight_Duplicates()

Dim lotarget As ListObject
Dim Name_Col, Dupl_Rng As Range
Dim Dupl_cell As Variant
Dim TableRows As Long
Dim First_Dupl As String


Set lotarget = Worksheets("Reconcile Meds Here").ListObjects("Table3")

TableRows = lotarget.ListRows.Count
Name_Col = lotarget.Range.Columns(1)


    
        For Each Dupl_cell In Name_Col
            If WorksheetFunction.CountIf(Name_Col, Dupl_cell.Value) > 1 Then
                Set Dupl_Rng = Rng.Find(What:=Dupl_cell.Value, LookIn:=xlValues)
                If Not Dupl_Rng Is Nothing Then
                    First_Dupl = Dupl_cell.Address
                    Do
                        Set Dupl_cell = Rng.FindNext(Dupl_cell)
                        Dupl_cell.Interior.ColorIndex = 35
                    Loop While Not Dupl_cell Is Nothing And Dupl_cell.Address <> First_Dupl
                End If
            End If
        Next

End Sub

Thanks for helping. VBA drives me NUTS!

Steve
 
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Either remove the .Value from Dupl_cell.Value or use Range objects with Set:
Code:
Sub Highlight_Duplicates()

Dim lotarget As ListObject
Dim Name_Col As Range, Dupl_Rng As Range
Dim Dupl_cell As Range
Dim TableRows As Long
Dim First_Dupl As String


Set lotarget = Worksheets("Reconcile Meds Here").ListObjects("Table3")

TableRows = lotarget.ListRows.Count
Set Name_Col = lotarget.Range.Columns(1)


    
        For Each Dupl_cell In Name_Col.Cells
            If WorksheetFunction.CountIf(Name_Col, Dupl_cell.Value) > 1 Then
                Set Dupl_Rng = Rng.Find(What:=Dupl_cell.Value, LookIn:=xlValues)
                If Not Dupl_Rng Is Nothing Then
                    First_Dupl = Dupl_cell.Address
                    Do
                        Set Dupl_cell = Rng.FindNext(Dupl_cell)
                        Dupl_cell.Interior.ColorIndex = 35
                    Loop While Not Dupl_cell Is Nothing And Dupl_cell.Address <> First_Dupl
                End If
            End If
        Next

End Sub

As an aside, note that this:
Code:
Dim Name_Col, Dupl_Rng As Range
declares Name_Col as Variant and Dupl_Range as Range. To declare both as Range you must specify the type for both:
Code:
Dim Name_Col As Range, Dupl_Rng As Range
 
Upvote 0
You can use conditional formatting. Click here to download book with example. It has Table with some dummy data. Press "Highlight First Unique" button to see the result.
Here's macro to do it. Adopt this code to your needs.
Code:
Sub HighlightFirstUnique()
    Dim lo As ListObject
    Set lo = ActiveSheet.ListObjects(1) 'Our list object (table)
    With lo.DataBodyRange.Columns(1)
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF(E$2:E2;E2)>1"
        .FormatConditions(1).Interior.Color = RGB(255, 0, 0)
    End With
End Sub
 
Upvote 0
I have corrected my code.
Code:
Sub HighlightFirstUnique()
    Dim lo As ListObject
    Set lo = ActiveSheet.ListObjects(1)
    With lo.DataBodyRange.Columns(1)
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF(" & .Cells(1).Address(1, 0) & ":" & .Cells(1).Address(0, 0) & ";" & .Cells(1).Address(0, 0) & ")>1"
        .FormatConditions(1).Interior.Color = RGB(255, 0, 0)
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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