问题描述
我正尝试在Excel中编写一个函数,该函数将根据存储在其他三个单元格中的值设置活动单元格的背景色(这三个单元格中的每个单元格存储从0到255的数值,具体取决于
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).
所以A1单元格为150,B1单元格为220,C1单元格为90(即RGB(150,220,90 ))。我需要D1单元格的颜色是之前声明的RGB(某种绿色),而且,如果我将函数放在D2中,它将选择存储在A2,B2和C2中的RGB,依此类推...
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...
可以实现吗?
推荐答案
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
美国ge(在D1中输入):
Usage (entered in D1):
=myRGB(A1,B1,C1)
这篇关于根据其他单元格中存储的RGB值动态更改单元格的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!