根据颜色比较两个单元格
Compare two cells based on their color
我想根据两个单元格的颜色来比较它们。
我创建了以下函数。如果单元格中的两种颜色匹配,则应写入 Same
并为单元格中的单元格着色 green
,否则为 'Change' 并为其着色 red
.
但是,我目前得到:
在 #Value
单元格中,我的公式是 =ColorComparer(H4;C4)
您可以在下面找到我创建的 vba 函数:
Function ColorComparer(rColor As Range, rRange As Range)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If rCell.Interior.ColorIndex = lCol Then
vResult = "Same"
vResult.Interior.ColorIndex = RGB(0,255,0)
Else
vResult = "Change"
vResult.Interior.ColorIndex = RGB(255,0,0)
End If
ColorComparer = vResult
End Function
非常感谢您的回复!
此致!
经过一些小改动,您的 UDF 给出了正确的 (Same/Change
) 结果:
Function ColorComparer(rColor As Range, rRange As Range) As String
Dim rCell As Range
Dim lCol As Long
Dim vResult as String
lCol = rColor.Interior.Color
If rRange.Interior.Color = lCol Then
vResult = "Same"
' vResult.Interior.ColorIndex = RGB(0, 255, 0)
Else
vResult = "Change"
' vResult.Interior.ColorIndex = RGB(255, 0, 0)
End If
ColorComparer = vResult
End Function
变更列表:
- 用
rRange
参数替换了 rCell
。 rCell
没有分配任何值
- 用完整的 RGB 值
Color
替换了 ColorIndex
- 注释了
vResult.Interior
行。 vResult 不指向当前单元格,即使指向,也有多个 limitations of UDFs:
A user-defined function called by a formula in a worksheet cell cannot change the environment of Microsoft Excel. This means that such a function cannot do any of the following:
Insert, delete, or format cells on the spreadsheet.
Change another cell's value.
Move, rename, delete, or add sheets to a workbook.
Change any of the environment options, such as calculation mode or
screen views.
Add names to a workbook.
Set properties or execute most methods.
您可以使用结果范围的条件格式来克服这些限制。
这个 UDF 最棘手的部分是它的重新计算。即使您将其定义为带有附加行的 volatile:
Application.Volatile True
更改单元格的背景不会触发重新计算。
我知道的唯一解决方案是在 sheet 的 SelectionChanged
和 Activate
事件处理程序中触发 sheet 重新计算。
我想根据两个单元格的颜色来比较它们。
我创建了以下函数。如果单元格中的两种颜色匹配,则应写入 Same
并为单元格中的单元格着色 green
,否则为 'Change' 并为其着色 red
.
但是,我目前得到:
在 #Value
单元格中,我的公式是 =ColorComparer(H4;C4)
您可以在下面找到我创建的 vba 函数:
Function ColorComparer(rColor As Range, rRange As Range)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If rCell.Interior.ColorIndex = lCol Then
vResult = "Same"
vResult.Interior.ColorIndex = RGB(0,255,0)
Else
vResult = "Change"
vResult.Interior.ColorIndex = RGB(255,0,0)
End If
ColorComparer = vResult
End Function
非常感谢您的回复!
此致!
经过一些小改动,您的 UDF 给出了正确的 (Same/Change
) 结果:
Function ColorComparer(rColor As Range, rRange As Range) As String
Dim rCell As Range
Dim lCol As Long
Dim vResult as String
lCol = rColor.Interior.Color
If rRange.Interior.Color = lCol Then
vResult = "Same"
' vResult.Interior.ColorIndex = RGB(0, 255, 0)
Else
vResult = "Change"
' vResult.Interior.ColorIndex = RGB(255, 0, 0)
End If
ColorComparer = vResult
End Function
变更列表:
- 用
rRange
参数替换了rCell
。rCell
没有分配任何值 - 用完整的 RGB 值
Color
替换了 - 注释了
vResult.Interior
行。 vResult 不指向当前单元格,即使指向,也有多个 limitations of UDFs:
ColorIndex
A user-defined function called by a formula in a worksheet cell cannot change the environment of Microsoft Excel. This means that such a function cannot do any of the following:
Insert, delete, or format cells on the spreadsheet.
Change another cell's value.
Move, rename, delete, or add sheets to a workbook.
Change any of the environment options, such as calculation mode or screen views.
Add names to a workbook.
Set properties or execute most methods.
您可以使用结果范围的条件格式来克服这些限制。
这个 UDF 最棘手的部分是它的重新计算。即使您将其定义为带有附加行的 volatile:
Application.Volatile True
更改单元格的背景不会触发重新计算。
我知道的唯一解决方案是在 sheet 的 SelectionChanged
和 Activate
事件处理程序中触发 sheet 重新计算。