1

我有一个电子表格 - 在其中一个选项卡上,我有一个填满名称的表格,它们与一组颜色编码的参数相关,并且它们具有不同的背景颜色。

我需要创建一个公式来将那些基于颜色的“X”更改为简单的文本(例如,如果一个单元格有绿色背景和黑色“X”,我想称之为 GB,如果单元格有黄色背景和蓝色“X” “-YB等)


更新:

我创建了两个名称范围:

细胞颜色:=GET.CELL(63,OFFSET(INDIRECT("RC",FALSE),0,-1))

字体颜色:=GET.CELL(24,OFFSET(INDIRECT("RC",FALSE),0,-1))

我已经计算出字体和背景颜色的数字。但是当我输入这个公式时,它没有返回正确的值:

=IF(AND(FontColor=3,I18="X"),"EXR",(IF(AND(FontColor=23,I18="X"),"BU",(IF(AND(Fo‌ntColor=0,I18="X"),"EPL",0)))))

如果我将公式放在单元格左侧的列中,它可以工作,如果在另一个选项卡上,它不会。

4

1 回答 1

1

在 VBA 中:

Sub SetValueBasedOnColors()
Dim c As Range
For Each c In Range("A2:A10")
    If c.Interior.Color = RGB(196, 215, 155) And c.Font.Color = RGB(0, 0, 0) Then
        c.Value = "GB"
    ElseIf c.Interior.Color = RGB(255, 255, 0) And c.Font.Color = RGB(31, 73, 125) Then
        c.Value = "YB"
    End If
Next c
End Sub

结果:

结果

您可以通过右键单击单元格并选择来获得内部颜色:

Format Cells...->More Colors...->Custom (Tab)

颜色2

颜色3

如果有很多颜色可供使用,您可以设置一个“颜色表”以保持代码简单,并让您不必查找每种颜色。

只需将带有颜色和 X 的单元格复制到一个范围中,然后输入您希望它们是什么。确保将下面的 ColorTable 范围更改为表格的正确范围。

Sub SetValueBasedOnColors()
Dim ColorTable As Range
Set ColorTable = Range("D2:D3") 'Point this range to your color table
Dim c As Range
Dim z As Range
For Each c In Range("A2:A10") 'Point this range to your data
    For Each z In ColorTable
        If c.Interior.Color = z.Interior.Color And c.Font.Color = z.Font.Color Then
            c.Value = z.Value
        End If
    Next z
Next c
End Sub

颜色4

结果:

颜色5

于 2015-06-18T17:49:43.943 回答