Excel VBA检查单元格颜色与Activecell.Interior.color不起作用

我想检查一个单元格是否有一定的颜色。 如果为true,我想要这个消息框(“单元格匹配颜色”)。 否则,我希望有消息框(“单元格不匹配颜色”)。

Option Explicit Sub Autoselect() Dim Refcolor As Long Set Refcolor = RGB(220, 230, 241) If ActiveCell.Interior.Color = Refcolor Then MsgBox ("Cell Match Color") Else: MsgBox ("Cell does not match color") End Sub 

当您将值赋给variablesRefcolor时,只需删除关键字Set

Set用于将对象分配给variables,并且您正在分配原始值。

 Sub Autoselect() Dim Refcolor As Long Refcolor = RGB(220, 230, 241) If ActiveCell.Interior.Color = Refcolor Then MsgBox ("Cell Match Color") Else MsgBox ("Cell does not match color") End If End Sub