2017-10-18 179 views
0

我想要根据数据表单元格颜色。例如,我有表是这样的: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 

现在,我需要让他们根据表2我想通了使用索引和列代表他们的状态,颜色D2.1地图,但我需要使它像在条件格式的东西:

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

这可能吗?

谢谢您的帮助

+0

这确实是条件格式化的一种情况。将一个填充颜色设置为“无颜色”,并沿着'= IF(A3 =“X”)的行建立3条应用另一种颜色的规则。然后填充颜色=红色并停止处理该单元格的其他规则。 – Variatus

+0

所以我们不能只是定义颜色,并以编程方式设置它? – Magician

+0

要做到这一点,你需要VBA – Variatus

回答

0

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

请遵守代码中的注释。

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 
+0

啊..好.. Interior.Color。尼斯。这是我需要的。我会将这些颜色图例与他们的状态一起放在另一张表中。 谢谢 – Magician

+0

是的。我了解代码。将做出一些调整以适应我的需要,但这是我失踪的一块。谢谢。 – Magician

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"

  • 做其他两种颜色的同样的事情,这里的公式供你使用:

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

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

    所以在最后,你应该有这样的事情如下:

  • 更改Applies to领域=$G$3:$H$6或范围,你想使用。点击ApplyOK,你应该得到你想要的结果。
  • +0

    是的,但问题是,其状态并非总是如此。我可以添加到表格中。我更喜欢使用VBA来“查看”颜色,另一个人建议使用VBA,并且我认为我也需要VBA来达到这个目的,并从数组中进行设置。 – Magician

    +0

    VBA可能是一种更好的方式。但是再想一想这里使用的VLOOKUP公式,您可以简单地将'X,S,O'点改为一个单元格,例如'= VLOOKUP(G3,$ D $ 3:$ E $ 7,2,0)= $ A $ 1',这样你可以随时更改状态码。它是由你决定。 – ian0411