Link to home
Start Free TrialLog in
Avatar of ka_inc
ka_incFlag for United States of America

asked on

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
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

It works for me without any problem..Can you post your sample file to look over to see whats wrong..
Avatar of ka_inc

ASKER

This is the sample file.
Thank you
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...
Avatar of ka_inc

ASKER

User generated imageSorry, even when I change dim cel as Range, I still get the Run-time error.
ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ka_inc

ASKER

Thank you