Excel VB | duplicate range highlight and remove
This post is a follow up to the unique random numbers post - I have included a Remove Duplicates from Range function, in the workbook here, so that you can check the list it creates. This function below will remove the first duplicate in a range that you select.
Sub RemoveFirstDuplicates()
Dim rConstRange As Range, rFormRange As Range
Dim rAllRange As Range, rCell As Range
Dim iCount As Long
Dim strAdd As StringOn Error Resume Next
Set rAllRange = Selection
If WorksheetFunction.CountA(rAllRange) < 2 Then
MsgBox “You selection is not valid”, vbInformation
On Error GoTo 0
Exit Sub
End If
Set rConstRange = rAllRange.SpecialCells(xlCellTypeConstants)
Set rFormRange = rAllRange.SpecialCells(xlCellTypeFormulas)
If Not rConstRange Is Nothing And Not rFormRange Is Nothing Then
Set rAllRange = Union(rConstRange, rFormRange)
ElseIf Not rConstRange Is Nothing Then
Set rAllRange = rConstRange
ElseIf Not rFormRange Is Nothing Then
Set rAllRange = rFormRangeElse
MsgBox “You selection is not valid”, vbInformation
On Error GoTo 0
Exit Sub
End IfApplication.Calculation = xlCalculationManual
For Each rCell In rAllRange
strAdd = rCell.Address
strAdd = rAllRange.Find(What:=rCell, After:=rCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).AddressIf strAdd <> rCell.Address Then
rCell.Clear
End IfNext rCell
Application.Calculation = xlCalculationAutomatic
On Error GoTo 0End Sub
This is another duplicate function - this one will count the duplicates and also highlight them in green (this was not included in download).
Sub CountNumberOfDuplicates()
On Error GoTo ENDER
Application.ScreenUpdating = FalseIf Selection.Columns.Count * Selection.Rows.Count = 1 Then
MsgBox “Select more then one cell.”, vbExclamation
Exit Sub
End IfSetColourIndex = 4
i = Selection.Cells.Count
n = 0
k = 0For Each MyCell In Selection
CellCount = MyCell.Value
If MyCell.Value = “” Then
ElseIf Application.WorksheetFunction.CountIf(Selection, CellCount) > 1 Then
k = k + 1
n = n + 1
MyCell.Interior.ColorIndex = SetColourIndexEnd If
End If
Percentage = n / i * 100
Next
Application.StatusBar = False
Application.ScreenUpdating = TrueMsgBox “Selection contains ” & k & ” duplicate values.”, vbInformation
Exit Sub
ENDER:
Application.StatusBar = False
Application.ScreenUpdating = TrueEnd Sub
Follow my updates on Twitter here
- Just found out about this designer Pedro Vilas-Boas yesterday - his ten_pt work blew me away: http://www.vilaz.tv/. (4 hrs ago)
About
You’re currently reading “Excel VB | duplicate range highlight and remove”.
- Published:
- Tuesday, August 19th, 2008
- Author:
- Simon Page
- Category:
- Excel
- Tags:
- downloads, excel 2003, excel 2007, free, vb, vba, visual basic
Related Posts
Categories
- Artwork (58)
- Design Inspiration (23)
- Design Resources (6)
- Excel (12)
- Filemaker (5)
- Gadgets (26)
- Music (3)
- Photography (3)
- Photoshop (1)
- Press (3)
- Random (5)
- Software (4)
- Video Gaming (15)
Featured Posts
- 20 of the Best Amazing Photoshop Tutorials
- Creative Movie Poster Inspiration
- December 09: Graphic Designs, Illustrations and looking back at 2009
- Design Inspiration | typography posters
- Fun with Circles poster designs
- Inspiring Gallery of Video Game Concept Art
- International Year of Astronomy 2009 Posters
- Resources of Creative Design Inspiration
- Rolet Design Concept
- Top 10 Gadgets for 2009
- Top Christmas Electronics
- Tron Legacy Movie Poster






No comments
Jump to comment form | comments rss | trackback uri