在VBA中创build一个颜色vector

我正在编写一个macros,通过多年的数据表来检查特定颜色的单元格。 不幸的是,这些年来人类吸食并没有一贯地select相同的细胞颜色(它们可能都与人眼相同,但具有不同的RGB值)。

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

我知道我可以使用IF语句来查看颜色是否在这两个值之间,但是如果我可以创build一个颜色vector,我可以使用它来获取更多的应用程序(如果需要的话,代码会更容易修改改变)。

这个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 

这应该做的:

 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