Tuesday, 22 June 2010

Highlight Duplicates in Excel 2003

In Excel 2003 there isn't a way to highlight duplicate items so i have written this VBA script to do it for you, just copy and past the script below. You will need to sort the data before you can run this script. You can change the values in the colour argument to change the colour that the cells will be high lighted in.

Sub FindDuplicates()
'
' !!NOTE: You MUST select the FIRST cell in the column and
' make sure that the column is SORTED before running this macro!!
'
Application.ScreenUpdating = False

colour = RGB(255, 0, 0)
FirstItem = ActiveCell.Value
SecondItem = ActiveCell.Offset(1, 0).Value
Offsetcount = 1
Do While ActiveCell <> ""
If FirstItem = SecondItem Then
ActiveCell.Offset(Offsetcount, 0).Interior.Color = colour
Offsetcount = Offsetcount + 1
SecondItem = ActiveCell.Offset(Offsetcount, 0).Value
Else
ActiveCell.Offset(Offsetcount, 0).Select
FirstItem = ActiveCell.Value
SecondItem = ActiveCell.Offset(1, 0).Value
Offsetcount = 1
End If
Loop

ActiveCell.Offset(-1, 0).Select

FirstItem = ActiveCell.Value
SecondItem = ActiveCell.Offset(-1, 0).Value
Offsetcount = -1
Do While ActiveCell <> ""
If FirstItem = SecondItem Then
ActiveCell.Offset(Offsetcount, 0).Interior.Color = colour
Offsetcount = Offsetcount - 1
SecondItem = ActiveCell.Offset(Offsetcount, 0).Value
Else
ActiveCell.Offset(Offsetcount, 0).Select
FirstItem = ActiveCell.Value
SecondItem = ActiveCell.Offset(Offsetcount, 0).Value
Offsetcount = -1
End If
Loop

Application.ScreenUpdating = True

End Sub

This macro will process one column at a time and it will process the entire column.

0 comments:

Post a Comment