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?
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..
0
ka_incAuthor Commented:
This is the sample file.
Thank you
0
ka_incAuthor Commented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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...
0
ka_incAuthor Commented:
Error.jpgSorry, even when I change dim cel as Range, I still get the Run-time error.
0
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...
0

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.