【问题标题】:Set background colour of cell to RGB value of data in cell将单元格的背景颜色设置为单元格中数据的 RGB 值
【发布时间】:2010-11-28 10:08:10
【问题描述】:

我有一列包含 RGB 值,例如:

127,187,199
67,22,94

在 Excel 中,有什么方法可以用来设置单元格的背景颜色?

【问题讨论】:

    标签: excel vba rgb


    【解决方案1】:
    Sub AddColor()
        For Each cell In Selection
            R = Round(cell.Value)
            G = Round(cell.Offset(0, 1).Value)
            B = Round(cell.Offset(0, 2).Value)
            Cells(cell.Row, 1).Resize(1, 4).Interior.Color = RGB(R, G, B)
        Next cell
    End Sub
    

    假设有 3 列 R、G 和 B(按此顺序)。选择第一列,即 R。按 alt+F11 并运行上面的代码。我们必须选择第一列(包含 R 或红色值)并在每次更改值以反映更改时运行代码。

    我希望这个更简单的代码有所帮助!

    【讨论】:

      【解决方案2】:

      要根据当前整数值为每个单元格着色,如果您使用的是最新版本的 Excel,则应使用以下方法。 (旧版本也不处理 rgb)

      Sub Colourise()
      '
      ' Colourise Macro
      '
      ' Colours all selected cells, based on their current integer rgb value
      ' For e.g. (it's a bit backward from what you might expect)
      ' 255 = #ff0000 = red
      ' 256*255 = #00ff00 = green
      ' 256*256*255 #0000ff = blue
      ' 255 + 256*256*255 #ff00ff = magenta
      ' and so on...
      '
      ' Keyboard Shortcut: Ctrl+Shift+C (or whatever you want to set it to)
      '
        For Each cell In Selection
          If WorksheetFunction.IsNumber(cell) Then
            cell.Interior.Color = cell.Value
          End If
        Next cell
      End Sub
      

      如果您有一个字符串而不是数字,那么您可以将字符串拆分为三个数字并使用 rgb() 组合它们。

      【讨论】:

        【解决方案3】:

        不能从用作工作表公式的 VBA 函数中更改单元格。除了通过这种解决方法...

        将此函数放入一个新模块中:

        Function SetRGB(x As Range, R As Byte, G As Byte, B As Byte)
          On Error Resume Next
          x.Interior.Color = RGB(R, G, B)
          x.Font.Color = IIf(0.299 * R + 0.587 * G + 0.114 * B < 128, vbWhite, vbBlack)
        End Function
        

        然后在工作表中使用此公式,例如在单元格D2

        =HYPERLINK(SetRGB(D2;A2;B2;C2);"HOVER!")
        

        将鼠标悬停在单元格上(试试看!)后,背景颜色会更新为从单元格 A2C2 的 RGB。字体颜色是对比鲜明的白色或黑色。

        【讨论】:

          【解决方案4】:

          单独设置 Color 属性将保证完全匹配。 Excel 2003 一次只能处理 56 种颜色。好消息是您可以将任何 rgb 值分配给这 56 个插槽(称为 ColorIndex)。当您使用 Color 属性设置单元格的颜色时,这会导致 Excel 使用最接近的“ColorIndex”。示例:将单元格设置为 RGB 10,20,50(或 3281930)实际上会导致其设置为颜色索引 56,即 51,51,51(或 3355443)。

          如果您想确保得到完全匹配,您需要将 ColorIndex 更改为您想要的 RGB 值,然后将 Cell 的 ColorIndex 更改为所述值。但是,您应该知道,通过更改颜色索引的值,您可以更改工作簿中已经使用该颜色的 所有 单元格的颜色。举个例子,红色是 ColorIndex 3。因此,您制作 Red 的任何单元格实际上都制作了 ColorIndex 3。如果您将 ColorIndex 3 重新定义为紫色,那么您的单元格确实会变成紫色,但所有其他红色单元格中工作簿也将变为紫色。

          有几种策略可以解决这个问题。一种方法是选择一个尚未使用的索引,或者只选择一个您认为不太可能使用的索引。另一种方法是更改​​ 最近的 ColorIndex 的 RGB 值,这样您的更改就会很微妙。我在下面发布的代码采用了这种方法。利用分配最近的 ColorIndex 的知识,它将 RGB 值直接分配给单元格(从而产生最接近的颜色),然后将 RGB 值分配给该索引。

          Sub Example()
              Dim lngColor As Long
              lngColor = RGB(10, 20, 50)
              With Range("A1").Interior
                  .Color = lngColor
                  ActiveWorkbook.Colors(.ColorIndex) = lngColor
              End With
          End Sub
          

          【讨论】:

          • 谢谢,这也很有帮助。
          【解决方案5】:

          您可以使用 VBA - 类似

          Range("A1:A6").Interior.Color = RGB(127,187,199)
          

          只需传入单元格值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-01-28
            • 2015-10-21
            • 2011-07-13
            • 1970-01-01
            • 2019-07-27
            • 1970-01-01
            • 2015-10-12
            相关资源
            最近更新 更多