Tuesday, December 13, 2011

How to Remove Duplicates from Excel Macro


Build a Macro to Remove First Column Duplicates
1. Browse to Tools on the toolbar and down to Macro. To the right, find the Visual Basic Editor and left-click on it. Once in the Visual Basic Editor, find the drop-down that allows you to create a User Form, Module or Class Module. Choose 'Module.'
2. Copy and paste the following into the editing box that opens:
Sub DelDups_OneList()
Dim iListCount As Integer
Dim iCtr As Integer
' Turn off screen updating to speed up macro.
Application.ScreenUpdating = False
' Get count of records to search through.
iListCount = Sheets('Sheet1').Range('A1:A100').Rows.Count
Sheets('Sheet1').Range('A1').Select
' Loop until end of records.
Do Until ActiveCell = ''
' Loop through records.
For iCtr = 1 To iListCount
' Don't compare against yourself.
' To specify a different column, change 1 to the column number.
If ActiveCell.Row
Sheets('Sheet1').Cells(iCtr, 1).Row Then
' Do comparison of next record.
If ActiveCell.Value = Sheets('Sheet1').Cells(iCtr, 1).Value Then
' If match is true then delete row.
Sheets('Sheet1').Cells(iCtr, 1).Delete xlShiftUp
' Increment counter to account for deleted row.
iCtr = iCtr 1
End If
End If
Next iCtr
' Go to next record.
ActiveCell.Offset(1, 0).Select
Loop
Application.ScreenUpdating = True
MsgBox 'Done!'
End Sub
3. Click 'File' and then 'Close and Return to Microsoft Excel.' Once returned to Excel, browse to the Tools option in the toolbar and then to Macros. To the right, choose the 'Macros' option. In the Macro option box, see the macro called DelDups_OneList. Choose 'Options' and assign a shortcut key of 'q.' Click 'OK.'
4. Click 'Run.' 'Done' will appear. The duplicate entries in the first column are deleted.
5. Because you have set up your shortcut key for Ctrl q, you only need to type Ctrl q in the future and the duplicate entries in your first column of data will be removed.

Blogger news