No duplicate cells in a column


Posted by David M on July 06, 2001 12:24 PM

How can you ensure that, within a given column, no two cells contain the same value?



Posted by Damon Ostrander on July 06, 2001 12:42 PM

Hi David,

There are lots of ways, but here is a macro that will delete any rows that contain duplicate entries in the column that is selected. If this is not exactly what you want, perhaps you will see how to modify it to do what you do want.

Happy computing.

Damon


Sub DeleteDuplicateRows()

' This macro deletes all rows on the active worksheet that
' have non-unique values in the selected column, including blanks.

Dim iRow As Long
Dim jRow As Long
Dim LastRow As Long
LastRow = Selection.Rows.Count
If ActiveSheet.UsedRange.Rows.Count < LastRow Then
LastRow = ActiveSheet.UsedRange.Rows.Count
End If
For iRow = LastRow To 1 Step -1
For jRow = 1 To iRow - 1
If Selection.Cells(iRow) = Selection.Cells(jRow) Then Rows(iRow).Delete
Next jRow
Next iRow

End Sub