Solved

VBA code that detects duplicates in a column

Posted on 2016-10-25
8
18 Views
Last Modified: 2016-10-25
Hi everyone,

I have a spreadsheet where one of the columns contains a list of names (both first and last names) and I've sorted the column so that it's in alphabetical order. Some of the rows in that column contain duplicate names so I was wondering if I could get some help with some vba code that would look down the column (across all rows) and see if it finds the same name and if it does then the consecutive rows would be highlighted a yellow colour? The same names might be in row 1 to 4 or it could be in rows 7 and 8 or any other consecutive rows.

The names would be in Column G.

Would really appreciate any help.
0
Comment
Question by:gwh2
  • 3
  • 3
  • 2
8 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41858532
Hi,

pls try
Sub Macro2()

    With Columns("G:G")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=COUNTIF(G1:$G$1,G1)>1"
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
    End With
End Sub

Open in new window

Regards
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41858587
Somehow, above code didn't work.  In that case, you might wanna try this alternative code:

Sub ColGYellow()

  maxrow = ActiveSheet.UsedRange.Rows.Count
  For i = 1 To maxrow Step 1
    If i <> maxrow Then
        If Cells(i, 7).Value = Cells(i + 1, 7).Value Then
            Cells(i + 1, 7).Interior.Color = 65535
        End If
    End If
  Next
  
End Sub

Open in new window

0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41858600
@KiMputer
Maybe because the formula must be a local formula.
On my computer, it works perfectly
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:gwh2
ID: 41858648
Thanks for the replies,

Rgonzo1971: I tried your code but it doesn't seems to work correctly, ie. for the first lot of 3 duplicate rows, it highlighted all 3 rows, for the second lot of 2 duplicate rows, it highlighted the second one but not the first, then for the next set of 2 duplicate rows, it highlighted all 2 rows. In another row, it highlighted a name which was not a duplicate.

Kimputer: I tried your code and it highlights all duplicates except the first one. I wondered is it possible for the code to highlight all the rows including the first one?

Thank you both so much for the code.
0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 250 total points
ID: 41858674
Include the first original turning yellow:

Sub ColGYellow()

  maxrow = ActiveSheet.UsedRange.Rows.Count
  For i = 1 To maxrow Step 1
    If i <> maxrow Then
        If Cells(i, 7).Value = Cells(i + 1, 7).Value Then
            Cells(i, 7).Interior.Color = 65535
            Cells(i + 1, 7).Interior.Color = 65535
        End If
    End If
  Next
  
End Sub

Open in new window

0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 41858777
then try
Sub Macro2()

    With Columns("G:G")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=COUNTIF(G:G,G1)>1"
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
    End With
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:gwh2
ID: 41859502
Thanks again - both blocks of code work great.
0
 
LVL 1

Author Closing Comment

by:gwh2
ID: 41859504
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question