Excel VBA: Match Cell Color

后端 未结 3 1623
长情又很酷
长情又很酷 2021-01-23 07:10

I have a workbook with two sheets. On Sheet A, I have changed the interior color of some cells. I would like to find cells in Sheet B with matching text and set them to have the

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 07:28

    To get exactly what I wanted, I used @tigeravatar's code as a base and ended up with the following:

    Sub MatchHighlight()
    
    Dim wsHighlight As Worksheet
    Dim wsData As Worksheet
    Dim rngColor As Range
    Dim rngFound As Range
    Dim KeywordCell As Range
    Dim strFirst As String
    Dim rngPicked As Range
    
    Set rngPicked = Application.InputBox("Select Cell", Type:=8)
    Set wsHighlight = Sheets("HR - Highlight")
    Set wsData = Sheets("Full List")
    
    With wsData.Columns("C")
        For Each KeywordCell In wsHighlight.Range("C2", wsHighlight.Cells(Rows.Count, "C").End(xlUp)).Cells
            Set rngFound = .Find(KeywordCell.Text, .Cells(.Cells.Count), xlValues, xlWhole)
            If Not rngFound Is Nothing Then
                strFirst = rngFound.Address
                Set rngColor = rngFound
                Do
                    Set rngColor = Union(rngColor, rngFound)
                    Set rngFound = .Find(KeywordCell.Text, rngFound, xlValues, xlWhole)
                Loop While rngFound.Address <> strFirst
    
                Set rngColor = rngColor.Offset(0, -2).Resize(1, 3)
    
                If KeywordCell.Interior.Color = rngPicked.Interior.Color Then
                    rngColor.Interior.Color = KeywordCell.Interior.Color
                End If
            End If
        Next KeywordCell
    End With
    
    End Sub
    

    Only real differences are that I let the user pick the color of cells they're trying to match, I only change the interior color when it matches the color picked, and I change the color of the whole row.

提交回复
热议问题