vba 根据存储在其他单元格中的 RGB 值动态更改单元格的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33179452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:02:40  来源:igfitidea点击:

Change a cell's background color dynamically according to a RGB value stored in other cells

excelvbacolorscell

提问by epaezr

I'm trying to write a function in Excel that will set the background color of the active cell according to the values stored in other three cells (each of those three cells store a numeric value from 0 to 255, depending on the color R, G or B).

我正在尝试在 Excel 中编写一个函数,该函数将根据存储在其他三个单元格中的值设置活动单元格的背景颜色(这三个单元格中的每一个都存储从 0 到 255 的数值,具体取决于颜色 R, G 或 B)。

So the A1 cell is 150, the B1 cell is 220 and the C1 cell is 90 (that's RGB(150, 220, 90)). I need that the D1 cell's color is that RGB declared before (some kind of green), and also, if I place the function in D2, it will select the RGB stored in A2, B2 and C2, and so on...

所以 A1 单元格为 150,B1 单元格为 220,C1 单元格为 90(即 RGB(150, 220, 90))。我需要 D1 单元格的颜色是之前声明的 RGB(某种绿色),而且,如果我将函数放在 D2 中,它将选择存储在 A2、B2 和 C2 中的 RGB,依此类推...

Can this be achieved?

这能实现吗?

回答by Tim Williams

UDF version:

UDF 版本:

Function myRGB(r, g, b)

    Dim clr As Long, src As Range, sht As String, f, v

    If IsEmpty(r) Or IsEmpty(g) Or IsEmpty(b) Then
        clr = vbWhite
    Else
        clr = RGB(r, g, b)
    End If

    Set src = Application.ThisCell
    sht = src.Parent.Name

    f = "Changeit(""" & sht & """,""" & _
                  src.Address(False, False) & """," & clr & ")"
    src.Parent.Evaluate f
    myRGB = ""
End Function

Sub ChangeIt(sht, c, clr As Long)
    ThisWorkbook.Sheets(sht).Range(c).Interior.Color = clr
End Sub

Usage (entered in D1):

用法(在 D1 中输入):

=myRGB(A1,B1,C1)

回答by Gary's Student

In D1enter:

D1 中输入:

=A1 & "," & B1 & "," & C1

and in the worksheet code area, enter the following event macro:

并在工作表代码区域中,输入以下事件宏:

Private Sub Worksheet_Calculate()
   Range("D1").Interior.Color = RGB(Range("A1"), Range("B1"), Range("C1"))
End Sub

enter image description here

在此处输入图片说明

回答by Excel Hero

Assuming you would want this to work with the entire columns instead of just row 1, here is the VBA procedure for the worksheet's code module:

假设您希望它与整个列一起工作,而不仅仅是第 1 行,这里是工作表代码模块的 VBA 过程:

Private Sub Worksheet_Change(ByVal Target As Range)

    With Target
        If .Count = 1 Then
            If .Column < 4 Then
                Cells(.Row, 4).Interior.Color = RGB(Cells(.Row, 1), Cells(.Row, 2), Cells(.Row, 3))
            End If
        End If
    End With

End Sub

Note: I do not know what you mean by the following and so have not addressed it: and also, if I place the function in D2, it will select the RGB stored in A2, B2 and C2.

注意:我不知道您所说的以下内容是什么意思,因此没有解决:and also, if I place the function in D2, it will select the RGB stored in A2, B2 and C2