简体   繁体   中英

Excel VBA check cell color with Activecell.Interior.color not working

I wish to check whether a cell has a certain color. If true, I want this message box ("Cell Match Color"). Otherwise, I wish to have Message box ("Cell does not match color.")

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

Just remove keyword Set when you assign value to variable Refcolor .

Set is used to assign objects to variable and you are assigning primitive value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM