+ Reply to Thread
Results 1 to 8 of 8

Find duplicaties based on cell colour

Hybrid View

  1. #1
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    Find duplicaties based on cell colour

    Hi guys,

    I'm having difficulties with creating a macro. The scenario: The macro needs to search in column I for a cell with a red background (Interior.ColorIndex = 3). Then it needs to search for duplicates of the value from that same cell in column I. If there's a match, then it needs to give it the same color.

    Example:
    Cell I1 until I13 have white background. Cell I14 has a red background with value 125. The macro needs to search in column I for value 125 and colour it red.

    I hope that you can help me.

    Cheers,

    Flunzy

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Find duplicaties based on cell colour

    If I'm understanding your request correctly, then the following should do the trick:

    
    Sub SpreadRed()
    Dim i As Long
    Dim LR As Long
    Dim RedNo As Long
    
    Application.ScreenUpdating = False
    LR = Cells(Rows.Count, "I").End(xlUp).Row
    For i = 2 To LR
        If Cells(i, "I").Interior.ColorIndex = 3 Then
            RedNo = Cells(i, "I").Value
            Exit For
        End If
    Next i
    Range("I1:I" & LR).AutoFilter Field:=1, Criteria1:=RedNo
    Range("I2:I" & LR).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
    ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
    End Sub
    Note that the procedure above presumes that your data in column I has a header...

  3. #3
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: Find duplicaties based on cell colour

    Quote Originally Posted by cantosh View Post
    If I'm understanding your request correctly, then the following should do the trick:

    
    Sub SpreadRed()
    Dim i As Long
    Dim LR As Long
    Dim RedNo As Long
    
    Application.ScreenUpdating = False
    LR = Cells(Rows.Count, "I").End(xlUp).Row
    For i = 2 To LR
        If Cells(i, "I").Interior.ColorIndex = 3 Then
            RedNo = Cells(i, "I").Value
            Exit For
        End If
    Next i
    Range("I1:I" & LR).AutoFilter Field:=1, Criteria1:=RedNo
    Range("I2:I" & LR).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
    ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
    End Sub
    Note that the procedure above presumes that your data in column I has a header...
    Dear Cantosh,

    Thanks for all the effort but it doesn't work 100%. It works for the first lines but not for the rest of the file. I've added an example file where you can test it. You will see that the code works from line 30 until 38 but it doesn't work on line 46, 85, 141 and 148.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Find duplicaties based on cell colour

    Quote Originally Posted by flunzy View Post
    It works for the first lines but not for the rest of the file.
    Thanks for the clarification! My initial understanding was that there would only be one red cell, which is why my initial effort only caught the first one. Shifting the order of a few of my lines should fix that:

    
    Sub SpreadRed()
    Dim i As Long
    Dim LR As Long
    Dim RedNo As Long
    
    Application.ScreenUpdating = False
    LR = Cells(Rows.Count, "I").End(xlUp).Row
    For i = 2 To LR
        If Cells(i, "I").Interior.ColorIndex = 3 Then
            RedNo = Cells(i, "I").Value
            Range("I1:I" & LR).AutoFilter Field:=1, Criteria1:=RedNo
            Range("I2:I" & LR).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
            ActiveSheet.AutoFilterMode = False
        End If
    Next i
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: Find duplicaties based on cell colour

    Quote Originally Posted by cantosh View Post
    Thanks for the clarification! My initial understanding was that there would only be one red cell, which is why my initial effort only caught the first one. Shifting the order of a few of my lines should fix that:

    
    Sub SpreadRed()
    Dim i As Long
    Dim LR As Long
    Dim RedNo As Long
    
    Application.ScreenUpdating = False
    LR = Cells(Rows.Count, "I").End(xlUp).Row
    For i = 2 To LR
        If Cells(i, "I").Interior.ColorIndex = 3 Then
            RedNo = Cells(i, "I").Value
            Range("I1:I" & LR).AutoFilter Field:=1, Criteria1:=RedNo
            Range("I2:I" & LR).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
            ActiveSheet.AutoFilterMode = False
        End If
    Next i
    Application.ScreenUpdating = True
    End Sub
    Thanks Cantosh for the new code. I'll keep yours as the second code for the case. Thanks for all the effort : ) Als a rep for you : D

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Find duplicaties based on cell colour

    Try
    Sub test()
        Dim r As Range
        With CreateObject("Scripting.Dictionary")
            For Each r In Range("i1", Range("i" & Rows.Count).End(xlUp))
                If r.Interior.ColorIndex = 3 Then
                    .Item(r.Value) = Empty
                Else
                    If .exists(r.Value) Then r.Interior.ColorIndex = 3
                End If
            Next
        End With
    End Sub
    or not first appeared cell is colored.
    Sub test()
        Dim r As Range, ff As String, x(), n As Long
        Application.ScreenUpdating = False
        With Application.FindFormat
            .Clear
            .Interior.ColorIndex = 3
        End With
        With Range("i1", Range("i" & Rows.Count).End(xlUp))
            Set r = .Find("*", searchformat:=True)
            If Not r Is Nothing Then
                ff = r.Address
                Do
                    n = n + 1: ReDim Preserve x(1 To n): x(n) = r.Value
                    Set r = Columns("i").Find("*", r, searchformat:=True)
                Loop Until ff = r.Address
                .AutoFilter 1, x, 7
                .Offset(1).Resize(.Rows.Count - 1).Interior.ColorIndex = 3
                .AutoFilter
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by jindon; 01-19-2017 at 11:35 AM.

  7. #7
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: Find duplicaties based on cell colour

    Quote Originally Posted by jindon View Post
    Try
    Sub test()
        Dim r As Range
        With CreateObject("Scripting.Dictionary")
            For Each r In Range("i1", Range("i" & Rows.Count).End(xlUp))
                If r.Interior.ColorIndex = 3 Then
                    .Item(r.Value) = Empty
                Else
                    If .exists(r.Value) Then r.Interior.ColorIndex = 3
                End If
            Next
        End With
    End Sub
    or not first appeared cell is colored.
    Sub test()
        Dim r As Range, ff As String, x(), n As Long
        Application.ScreenUpdating = False
        With Application.FindFormat
            .Clear
            .Interior.ColorIndex = 3
        End With
        With Range("i1", Range("i" & Rows.Count).End(xlUp))
            Set r = .Find("*", searchformat:=True)
            If Not r Is Nothing Then
                ff = r.Address
                Do
                    n = n + 1: ReDim Preserve x(1 To n): x(n) = r.Value
                    Set r = Columns("i").Find("*", r, searchformat:=True)
                Loop Until ff = r.Address
                .AutoFilter 1, x, 7
                .Offset(1).Resize(.Rows.Count - 1).Interior.ColorIndex = 3
                .AutoFilter
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    Hi Jindon,

    Once again, you save the day : D thanks a lot : ) the code works like a charm. Reputation point is coming your way : D

  8. #8
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: Find duplicaties based on cell colour

    double post....

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. conditional formatting fill cells based on text, how to find sum based on CF colour
    By tubbybear in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-27-2016, 12:04 PM
  2. How to change cell colour, if the colour is based on value from formula?
    By darah237 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-19-2016, 11:40 AM
  3. Replies: 6
    Last Post: 04-11-2016, 09:48 AM
  4. [SOLVED] Search cells based on student name, cell background colour and return a tally for colour
    By drof_06 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-26-2016, 04:31 AM
  5. Change colour of cells based on another cell's colour (Not value)
    By LTrain89 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2013, 08:44 PM
  6. Colour change column chart based on cell colour
    By Alice21 in forum Excel General
    Replies: 11
    Last Post: 04-05-2011, 10:10 AM
  7. Find and Add based on Font Colour
    By seanyeap in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-16-2009, 08:32 AM

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