Actually it would be easier to sort the range you wish to remove duplicates for and write a routine to check and see if the next line matches then delete the line you don't need in the loop. Played with the problem a while and made a simple module that checks current selected column for duplicates assuming the data is presorted.
Remember that you can sort ranges by using "Range(sMyRange).Sort" to automate the sorting. Note that I only allow for 1 blank line in the determining column.
Select a cell. The selected cell's row and column becomes the start row and the source column.
Here's the simple example I made that can be put into a module:
------------------------------------------------------------------------------
Public Sub DeleteDuplicates()
'Checks Current column for duplicates that's sorted and deletes the duplicate rows
Dim iRowOn As Long, iCheckedColumn As Long, iBlanklines As Integer
Dim vCurrentContent As Variant
iCheckedColumn = ActiveCell.Column
iRowOn = ActiveCell.Row
vCurrentContent = UCase(Cells(RowOn, CheckedColumn).Value)
iRowOn = iRowOn + 1
Do While iBlanklines < 2
If InvalidOrBlank(iRowOn, iCheckedColumn) Then
iBlanklines = iBlanklines + 1
iRowOn = iRowOn + 1
Else
If UCase(Cells(iRowOn, iCheckedColumn).Value & " ") = vCurrentContent & " " Then
Cells(iRowOn, iCheckedColumn).EntireRow.Delete
Else
vCurrentContent = UCase(Cells(iRowOn, iCheckedColumn).Value)
iRowOn = iRowOn + 1
End If
End If
Cells(iRowOn, iCheckedColumn).Activate
Loop
End Sub
Private Function InvalidOrBlank(iPassedRow As Long, iPassedColumn As Long) As Boolean
'If cell is invalid or blank returns True
InvalidOrBlank = True
On Error GoTo ExitInvalidOrBlank
If Cells(iPassedRow, iPassedColumn).Value & " " <> " " Then InvalidOrBlank = False
ExitInvalidOrBlank:
End Function
------------------------------------------------------------------------------
Hope this helps. If you want auto sorting and need help with the sort function let me know.
|