Run-time error with VBA code in Excel 2010

Have the typical code to highlight duplicate cells with custom formatting:  [$-409]m/d/yyyy h:mm:ss AM/PM;@

For Each cel In myrng
           If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
              If WorksheetFunction.CountIf(Range("H2:H" & cel.Row), cel) = 1 Then
                 cel.Interior.ColorIndex = clr
              Else
                 cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex
              End If
          End If
       Next

The code will highlight the original cell but will throw the Run-time error 1004 "Unable to get the Match property of Worksheetfunction class" on the duplicate cell.

Why is it doing this and how to fix.

Thank you
ka_incAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
It works for me without any problem..Can you post your sample file to look over to see whats wrong..
ka_incAuthor Commented:
This is the sample file.
Thank you
ka_incAuthor Commented:
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Saurabh Singh TeotiaCommented:
In your code you have declaration this..

dim cel as variant

Open in new window


change this to

        Dim cel As Range

Open in new window


This will take care of this error..

Saurabh...
ka_incAuthor Commented:
Error.jpgSorry, even when I change dim cel as Range, I still get the Run-time error.
Saurabh Singh TeotiaCommented:
My bad..you have to make one more changes in the code... Enclosed is your revised code which works...

 Dim cel As Range
        Dim myrng As Range
        Dim clr As Long

        Set myrng = Range("H2:H" & Range("H65536").End(xlUp).Row)
        myrng.Interior.ColorIndex = xlNone
        clr = 6

        For Each cel In myrng
           If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
              If WorksheetFunction.CountIf(Range("H2:H" & cel.Row), cel) = 1 Then
                 cel.Interior.ColorIndex = clr
              Else
                 cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel, myrng, False), 1).Interior.ColorIndex
              End If
          End If
       Next

Open in new window


Saurabh...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ka_incAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.