Excel单元格颜色格式

时间:2017-10-18 01:32:23

标签: excel

我想根据数据表制作单元格颜色。例如,我有这样的表:

O | OK
X | Error
S | Slow
U | Unchecked

然后我有另一张桌子,价值如下:

D2.1 | U
D2.2 | X
D2.3 | X
D2.4 | S
D2.5 | O

而且,我想要一张地图,例如:

Room 1
D2.1 |      
D2.2 | D2.3       

Room 2
D2.4 | D2.5

现在,我需要在地图上为D2.1着色,以便它们根据表2表示它们的状态。我已经想出了使用索引和列,但我需要在条件格式化中使用它:

=Switch(Index(Status[ID], Match(X1, Status[Status],0)), "O", "Green", "X", "Red", "S", "Yellow", "U", "White")

这可能吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

以下是您使用Conditional Formatting的示例,您只需要设置一次,它就能很好地为您工作,以下是您的工作方式:

  1. 在我的示例中,突出显示cell G3,然后转到Home > Conditional Formatting > New Rule > Use a formula ...并输入下面的公式并选择您想要的Fill颜色(请注意,您需要调整相应的范围,或者只使用named range,以便将来更容易管理。

    =VLOOKUP(G3,$D$3:$E$7,2,0)="X"

    1. 对其他两种颜色做同样的事情,以下是您可以使用的公式:

      黄色:=VLOOKUP(G3,$D$3:$E$7,2,0)="S"

      格林:=VLOOKUP(G3,$D$3:$E$7,2,0)="O"

      所以最后,你应该有以下内容:

      1. Applies to字段更改为=$G$3:$H$6或您要使用的范围。点击ApplyOK,即可获得所需的结果。

答案 1 :(得分:0)

以下代码必须安装在您拥有房间地图的工作表的代码表中。范围RoomMap在这里定义为A28:E32,可能会更改为包含您的表格。请注意,代码需要一个命名范围" Status"可以用其他现有表替换。

请注意代码中的注释。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 18 Oct 2017

    Dim RoomMap As Range
    Dim Stat As String                      ' new value (after change)
    Dim Cols()                              ' array of available colours
    Dim Col As Long                         ' selected colour

    ' Any cell in RoomMap will be coloured when changed.
    ' You can specify the range in any way a range can be specified.
    Set RoomMap = Range(Cells(28, "A"), Cells(32, "E"))

    ' ====================================================
    ' For the purpose of this test I have set up a Named Range
    ' called "Status", containing the letters O X S U.
    ' Each of the cells in RoomMap has Data Validation,
    ' also referring to the "Status" range,
    ' limiting entry to one of these letters or blank.
    ' ====================================================

    ' ====================================================
    ' The Cols() array contains Long values:
    '       vbBlue (0) = 16711680
    '       vbGreen (1) = 65280
    '       vbRed (2) = 255
    '       vbYellow (3) = 65535
    '       vbWhite (4) = 16777215
    ' They are matched to the sequence of letters in "Status"
    ' with (0) added to mark cells left blank

    ' You can replace the constant names with their numeric values.
    ' To define another colour, set the colour as fill in any cell,
    ' select the cell and write the following code in the Immediate window:-
    ' ? ActiveCell.Interior.Colour
    ' upon enter, the selected colour's number will be displayed.
    ' ====================================================


    If Not Application.Intersect(RoomMap, Target) Is Nothing Then
        Cols = Array(vbBlue, vbGreen, vbRed, vbYellow, vbWhite)
        With Target
            Stat = Trim(.Value)
            On Error Resume Next
            ' an error will occur if the cell doesn't have one
            ' of the values in "Status"
            ' The setting of Data Validation in the cell prevents
            ' any alternative value other than blank.
            Col = Application.Match(Target.Value, Range("Status"), 0)
            .Interior.Color = Cols(Col)
        End With
    End If
End Sub