簡體   English   中英

Excel單元格顏色格式

[英]Excel cell color formatting

我想根據數據表制作單元格顏色。 例如,我有這樣的表:

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表示它們的狀態。我已經找到了使用Index和column的方法,但是我需要使其類似於條件格式:

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

那可能嗎?

謝謝您的幫助

這是供您使用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或您要使用的范圍。 單擊“ Apply或“ OK ,您應該獲得所需的結果。

必須在具有房間圖的工作表的代碼表中安裝以下代碼。 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM