【问题标题】:Set a cell colour based on given/referenced R, G, B values根据给定/引用的 R、G、B 值设置单元格颜色
【发布时间】:2020-03-23 14:24:08
【问题描述】:

我想将一个函数放入一个单元格 =Colour(R, G, B),其中 R、G 和 B 是选定的参考单元格(例如 =Colour(A1, A2, A3) )代表红/绿/蓝色值。在单元格中填写此函数后,我希望它为单元格着色相应的 RGB 颜色。

在这里查看了许多线程,条件格式不会执行此操作,并且似乎 UDF 无法在没有狡猾解决方法的情况下编辑单元格属性存在问题。

在 64 位上运行 Excel,必要时设置 Windows 10。

我正在从事材料科学工作的颜色分析工作。如果有一个或多个单元格采用 RGB 颜色,我可以从我的样本的 CIELab 颜色分析中获得,那就太好了。

【问题讨论】:

  • RGB(100,100,100)
  • Excel Used-Defined-Functions : "Excel 不允许用 VBA 编写的 UDF 更改任何内容,除了输入它的单元格的值。
  • 考虑添加一些你正在尝试做什么的图片,总是很有帮助的
  • 那些狡猾的解决方法是使用子例程之外的唯一方法。
  • @ScottCraner 你能告诉我如何使用子程序来做到这一点吗?

标签: excel vba colors formatting


【解决方案1】:

试试这个:

Public Function PPrintRGBColor(RedComp As Integer, GreenComp As Integer, BlueComp As Integer) As String
ActiveCell.Interior.Color = RGB(RedComp, GreenComp, BlueComp)
End Function

您将获得所选单元格的颜色。只需根据您的要求进行调整。 如果您更喜欢使用宏,那将很有用:

Sub GetRGBColor()

Dim HEXColor As String
Dim RGBColor As String

HEXColor = Right("000000" & Hex(ActiveCell.Interior.Color), 6)
MsgBox Hex(ActiveCell.Interior.Color)
MsgBox HEXColor

RGBColor = "RGB (" & CInt("&H" & Right(HEXColor, 2)) & _
", " & CInt("&H" & Mid(HEXColor, 3, 2)) & _
", " & CInt("&H" & Left(HEXColor, 2)) & ")"

MsgBox RGBColor, vbInformation, "Cell " & ActiveCell.Address(False, False) & ":  Fill Color"

End Sub

此宏将为您提供 RGB 上选定单元格的颜色值。

Sub PrintRGBColor()

Dim RedComp As Integer
Dim GreenComp As Integer
Dim BlueComp As Integer

RedComp = Application.InputBox(prompt:="Red Component Value (0-255)", Type:=2)
GreenComp = Application.InputBox(prompt:="Green Component Value (0-255)", Type:=2)
BlueComp = Application.InputBox(prompt:="Blue Component Value (0-255)", Type:=2)

ActiveCell.Interior.Color = RGB(RedComp, GreenComp, BlueComp)

End Sub

这将让您通过一个选定单元格上的组件获得一种颜色。

如果您需要其他帮助,请告诉我们。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多