+ Reply to Thread
Results 1 to 13 of 13

highlight duplicates

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    29

    highlight duplicates

    I ve got this code.


    Sub zad3()
    Dim iWarnColor As Integer
    Dim rng As Range
    Dim rngCell As Variant
    
    
    
    
    Set rng = Range("B5:E30")
    
    
    
    For Each rngCell In rng.Cells
    vVal = rngCell.Text
    If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
    rngCell.Font.Color = RGB(255, 0, 0)
    Else
    rngCell.Font.Bold = True
    End If
    Next rngCell
    
    
    End Sub
    Hey,


    I want to highlight the first unique values in red and the duplicates of it in bold. Currently I highlight all duplicates. What should I change? Thanks
    Last edited by arlu1201; 03-05-2013 at 05:25 AM. Reason: Code tags not php tags.

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,605

    Re: highlight duplicates

    Please add code tags

  3. #3
    Registered User
    Join Date
    01-26-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: highlight duplicates

    any ideas?

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: highlight duplicates

    You want this to happen in columns B to E or only one specific column?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Registered User
    Join Date
    01-26-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: highlight duplicates

    I ve got a table which starts from B5 do E30. So the respective columns in the table are the columns from B to E.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: highlight duplicates

    Will it always be one pair of duplicates or are there chances of having more than 1 duplicates?

    Maybe you can attach a sample sheet to make it clear.

  7. #7
    Registered User
    Join Date
    01-26-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: highlight duplicates

    There is a chance of having more than one duplicate. I want the first value to be colored in red and the duplicates (no matter how many) should be in bold. That's it. The code I ve got colors all duplicates.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: highlight duplicates

    Do you have a sample file that you can upload?

  9. #9
    Registered User
    Join Date
    01-26-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: highlight duplicates

    Unfortunately, currently that's not possible. Its not that difficult , though, to visualize what I want and create a simple database to test..

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: highlight duplicates

    Try this code
    Option Explicit
    
    Sub highlight_dups()
    Dim j As Long, i As Long
    
    Application.ScreenUpdating = False
    
    With Worksheets("Sheet1")
        For j = 2 To 5
            For i = 5 To 30
                If .Cells(i, j).Value = .Cells(i + 1, j).Value And .Cells(i - 1, j).Value <> .Cells(i, j).Value Then
                    .Cells(i, j).Font.Color = RGB(255, 0, 0)
                    .Cells(i + 1, j).Font.Bold = True
                ElseIf .Cells(i, j).Value = .Cells(i + 1, j).Value And .Cells(i - 1, j).Value = .Cells(i, j).Value Then
                    .Cells(i + 1, j).Font.Bold = True
                End If
            Next i
        Next j
    End With
    
    MsgBox "Done"
    
    Application.ScreenUpdating = True
    
    End Sub

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: highlight duplicates

    I can do that, with no issues.

    But sometimes it happens that the code i provide works on my file but not on yours.

    Anyways, i will provide the code here shortly. Try it and let me know.

  12. #12
    Registered User
    Join Date
    01-26-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: highlight duplicates

    Sure, thanks a lot!

  13. #13
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: highlight duplicates

    Hi dulitu19,

    Post back re how this goes:

    Option Explicit
    Sub Macro1()
    
        'Written by Trebor76
        'Visit my website www.excelguru.net.au
    
        Dim clnUniqueValues As New Collection
        Dim rngMyData As Range
        Dim rngCell As Range
        
        Set rngMyData = Range("B5:E30")
        
        Application.ScreenUpdating = False
        
        For Each rngCell In rngMyData
            If Evaluate("COUNTIF(" & rngMyData.Address & "," & rngCell.Address & ")") > 1 Then
                On Error Resume Next
                    clnUniqueValues.Add rngCell.Value, CStr(rngCell.Value)
                    If Err.Number = 0 Then 'First entry from a block of entries
                        rngCell.Interior.Color = RGB(255, 0, 0)
                    Else
                        rngCell.Font.Bold = True
                    End If
                    Err.Clear
                On Error GoTo 0
            End If
        Next rngCell
        
        Set rngMyData = Nothing
        Set rngCell = Nothing
        Set clnUniqueValues = Nothing
        
        Application.ScreenUpdating = True
        
        MsgBox "Process is now complete"
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1