【问题标题】:Create a color vector in VBA在 VBA 中创建颜色矢量
【发布时间】:2017-06-01 15:31:23
【问题描述】:

我想查看多年的数据表以找到特定颜色的单元格。

多年来,人类并没有始终如一地选择相同的细胞颜色(它们在人眼看来可能都相同,但具有不同的 RGB 值)。

如果我有一个内部颜色为 RGB(255,23,50) 的单元格,有没有办法创建一个颜色向量来查看单元格的内部颜色是否落在它上面?我正在寻找一个具有 +/- 15 个 RGB 点的向量,所以如果我正在搜索具有 RGB(255,23,50) 的单元格,我想要一个介于 RGB(255,38,65) 和 RGB(240,8) 之间的向量,35)。

我可以使用 IF 语句来查看颜色是否介于这两个值之间,但我可以将颜色向量用于更多应用程序(如果需要更改代码会更容易修改)。

这个 if 语句有效:

If ActiveWorkbook.Worksheets("Sheet1").Range("e5").Interior.Color >= RGB(240, 8, 35) And ActiveWorkbook.Worksheets("Sheet1").Range("e5").Interior.Color <= RGB(255, 38, 65) Then
    MsgBox ("yes")
Else
    MsgBox ("no")
End If

我正在寻找更多类似的东西:

dim redVector as long ' or other appropriate variable type

' ***** code that defines the red vector *****

if range("e5").interior.color = redVector then
    ' do stuff
end if

【问题讨论】:

  • 一个想法是为每个 r、g 和 b 从 1 循环到 255,将它们转换为 long 并检查值变化中的相关性。

标签: excel vba colors


【解决方案1】:

应该这样做:

Function IsInVector(srcColor, newColor, lOffset As Long) As Boolean

    Dim lSrcColor As Long
    Dim lNewColor As Long
    Dim lTemp     As Long

    lSrcColor = CLng(srcColor)
    lNewColor = CLng(newColor)

    lTemp = (lSrcColor - lNewColor) / 65536
    lTemp = Abs(Round(lTemp, 0))

    If lOffset <> lTemp Then
        IsInVector = False
    Else
        IsInVector = True
    End If

End Function

'/ Example usage::::  
Sub test()

    Debug.Print IsInVector(RGB(255, 23, 50), RGB(255, 38, 65), 15) '~~~> True
    Debug.Print IsInVector(RGB(255, 23, 50), RGB(255, 43, 63), 15) '~~~> False
    Debug.Print IsInVector(RGB(255, 23, 50), RGB(255, 38, 65), 15) '~~~> True

End Sub

【讨论】:

  • 谢谢@cyboashu。我进行了更改,使其显示为If lOffset &gt; lTemp then
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多