简体   繁体   中英

Change colour of cells based on colour of another call. Excel

I have the following

Table 1
name Colour
James  red
John   blue

I also have

table 2
name
James
John

I need to colour the cell in Table 2 with the colour used in table 1. So I need to find the name in table 2 that matches the name in table 1, then match it up with the colour value associated to the name (always the value on the right).

Any help would be great thanks.

Column E is the start of your names and Column A:B is the name & colour lookup. Change these to named ranges / different sheets as appropriate.

Sub ColourCheck()

    Dim cell As Range
    Dim cellColour As String

    For Each cell In Sheet1.Range("E1").CurrentRegion.Rows

        cellColour = Application.WorksheetFunction.VLookup(cell.Value, Sheet1.Range("A1").CurrentRegion, 2, 0)

        Select Case cellColour

            Case "red":     cell.Interior.ColorIndex = 3
            Case "blue":    cell.Interior.ColorIndex = 5

        End Select

    Next cell

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM