Macros

Macros

Wally's Home Page | Functions | More Functions | Macros | Charts | Thermometer Chart | Functions (A-M) | Functions (N-Z)
  
    

Macros

If you are not familiar with using macros, it's a great way to automate time intensive chores. A lot of the work I do in Excel is information imported from an external database. Occassionally this information will contain duplicate entries. This macro will lookup duplicates and delete all but the original row entry.

The body of the macro is:

Public Sub DeleteDuplicateRows()
'
' This macro deletes duplicate rows in the selection. Duplicates are
' counted in the COLUMN of the active cell.

Dim Col As Integer
Dim R As Long
Dim C As Range
Dim N As Long
Dim v As Variant
Dim Rng As Range

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Col = ActiveCell.Column

If Selection.Rows.Count > 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If

N = 0
For R = Rng.Rows.Count To 1 Step -1
v = Rng.Cells(R, 1).Value
If Application.WorksheetFunction.CountIf(Rng.Columns(1), v) > 1 Then
Rng.Rows(R).EntireRow.Delete
N = N + 1
End If
Next R

EndMacro:

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub