PDA

View Full Version : Highlighting Duplicate numbers in excel only when they are next to each other



plawrenz
06-18-2010, 02:15 PM
I have a table that I want to either want to filter or copy the duplicates to a new worksheet when there is duplicate data in a certain column but only when they are next to each other. For example the 6112 026874784 is the same but I only care when they are next to each other :

Materials 6112 026874784 American International Group, Inc.
Financials 6118 591708102 MetroPCS Communications, Inc.
Financials 6119 756577102 Red Hat, Inc.
Materials 6112 026874784 American International Group, Inc.
Materials 6112 026874784 American International Group, Inc.

I tried a custom filter but it show all of the cells that have more than one
one in the column and I only care if they are next to each other. Thanks!

Range("G:G").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF(G:G,G1)>1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriori ty
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With

Simon Lloyd
06-18-2010, 02:29 PM
Does this do what you need?
Sub copy_duplicates()
Dim i As Long
For i = Sheets("Sheet1").Range("G" & Rows.Count).End(xlUp).Row To 2 Step -1
If Sheets("Sheet1").Range("G" & i).Value = Sheets("Sheet1").Range("G" & i - 1).Value Then
Sheets("Sheet1").Rows(i - 1 & ":" & i).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End If
Next i
End Sub

plawrenz
06-18-2010, 02:42 PM
Yep that worked! Thanks so Much!!!!!