{"id":251,"date":"2008-08-19T03:00:22","date_gmt":"2008-08-19T02:00:22","guid":{"rendered":"http:\/\/simoncpage.co.uk\/blog\/?p=251"},"modified":"2008-10-19T14:54:44","modified_gmt":"2008-10-19T13:54:44","slug":"excel-vb-duplicate-range-highlight-and-remove","status":"publish","type":"post","link":"https:\/\/simoncpage.co.uk\/blog\/2008\/08\/excel-vb-duplicate-range-highlight-and-remove\/","title":{"rendered":"Excel VB | duplicate range highlight and remove"},"content":{"rendered":"<p>This post is a follow up to the unique random numbers post &#8211; I have included a Remove Duplicates from Range function, in the workbook <a href=\"https:\/\/simoncpage.co.uk\/blog\/2008\/08\/19\/excel-vb-unique-random-numbers\/\" target=\"_blank\">here<\/a>,\u00a0so that you can check the list it creates. This function below will remove the first duplicate in a range that you select.<\/p>\n<blockquote><p>Sub RemoveFirstDuplicates()<\/p>\n<p>Dim rConstRange As Range, rFormRange As Range<br \/>\nDim rAllRange As Range, rCell As Range<br \/>\nDim iCount As Long<br \/>\nDim strAdd As String<\/p>\n<p>On Error Resume Next<br \/>\nSet rAllRange = Selection<br \/>\nIf WorksheetFunction.CountA(rAllRange) &lt; 2 Then<br \/>\nMsgBox &#8220;You selection is not valid&#8221;, vbInformation<br \/>\nOn Error GoTo 0<br \/>\nExit Sub<br \/>\nEnd If<br \/>\nSet rConstRange = rAllRange.SpecialCells(xlCellTypeConstants)<br \/>\nSet rFormRange = rAllRange.SpecialCells(xlCellTypeFormulas)<br \/>\nIf Not rConstRange Is Nothing And Not rFormRange Is Nothing Then<br \/>\nSet rAllRange = Union(rConstRange, rFormRange)<br \/>\nElseIf Not rConstRange Is Nothing Then<br \/>\nSet rAllRange = rConstRange<br \/>\nElseIf Not rFormRange Is Nothing Then<br \/>\nSet rAllRange = rFormRange<\/p>\n<p>Else<br \/>\nMsgBox &#8220;You selection is not valid&#8221;, vbInformation<br \/>\nOn Error GoTo 0<br \/>\nExit Sub<br \/>\nEnd If<\/p>\n<p>Application.Calculation = xlCalculationManual<\/p>\n<p>For Each rCell In rAllRange<br \/>\nstrAdd = rCell.Address<br \/>\nstrAdd = rAllRange.Find(What:=rCell, After:=rCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address<\/p>\n<p>If strAdd &lt;&gt; rCell.Address Then<br \/>\nrCell.Clear<br \/>\nEnd If<\/p>\n<p>Next rCell<\/p>\n<p>Application.Calculation = xlCalculationAutomatic<br \/>\nOn Error GoTo 0<\/p>\n<p>End Sub<\/p><\/blockquote>\n<p>This is another duplicate function &#8211; this one will count the duplicates and also highlight them in green (this was not included in download).<\/p>\n<blockquote><p>Sub CountNumberOfDuplicates()<\/p>\n<p>On Error GoTo ENDER<br \/>\nApplication.ScreenUpdating = False<\/p>\n<p>If Selection.Columns.Count * Selection.Rows.Count = 1 Then<br \/>\nMsgBox &#8220;Select more then one cell.&#8221;, vbExclamation<br \/>\nExit Sub<br \/>\nEnd If<\/p>\n<p>SetColourIndex = 4<\/p>\n<p>i = Selection.Cells.Count<br \/>\nn = 0<br \/>\nk = 0<\/p>\n<p>For Each MyCell In Selection<\/p>\n<p>CellCount = MyCell.Value<br \/>\nIf MyCell.Value = &#8220;&#8221; Then<br \/>\nElse<\/p>\n<p>If Application.WorksheetFunction.CountIf(Selection, CellCount) &gt; 1 Then<\/p>\n<p>k = k + 1<br \/>\nn = n + 1<br \/>\nMyCell.Interior.ColorIndex = SetColourIndex<\/p>\n<p>End If<\/p>\n<p>End If<\/p>\n<p>Percentage = n \/ i * 100<\/p>\n<p>Next<\/p>\n<p>Application.StatusBar = False<br \/>\nApplication.ScreenUpdating = True<\/p>\n<p>MsgBox &#8220;Selection contains &#8221; &amp; k &amp; &#8221; duplicate values.&#8221;, vbInformation<br \/>\nExit Sub<br \/>\nENDER:<br \/>\nApplication.StatusBar = False<br \/>\nApplication.ScreenUpdating = True<\/p>\n<p>End Sub<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This post is a follow up to the unique random numbers post &#8211; I have included a Remove Duplicates from Range function, in the workbook here,\u00a0so 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[26],"tags":[123,79,78,117,27,119,121],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/251"}],"collection":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":0,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"wp:attachment":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}