vba基于单元格颜色的msgbox单元格

时间:2017-09-05 13:55:44

标签: vba excel-vba colors cell msgbox

我打算在打开电子表格时为msgbox创建一个带有红色填充的所有单元格的宏,我现在有以下但是它甚至没有显示msgbox,任何想法?

Sub test()

Dim ws As Worksheet
Set ws = Sheets("Month End Tasks")
Dim i As Integer
i = 1
Do Until i = 11
    If ws.Range("C" & i).Interior.Color = RGB(255, 0, 0) Then
        MsgBox "C" & i & "  is red!!"
    End If
i = i + 1
Loop

End Sub

谢谢,

1 个答案:

答案 0 :(得分:2)

尝试使用以下内容查看C1至C11的RGB颜色:

Sub WriteCellColor()

    Dim ws      As Worksheet
    Dim i       As Long

    Set ws = ActiveSheet

    i = 1
    Do Until i = 11
        Debug.Print ws.Range("C" & i).Address
        Debug.Print getRGB2(ws.Range("C" & i).Interior.Color)
        i = i + 1
    Loop

End Sub

Public Function getRGB2(l_long) As String

    Dim c   As Long
    Dim R   As Long
    Dim G   As Long
    Dim B   As Long

    c = l_long
    R = c Mod 256
    G = c \ 256 Mod 256
    B = c \ 65536 Mod 256
    getRGB2 = "R=" & R & ", G=" & G & ", B=" & B

End Function

它将在即时窗口中打印RGB颜色的摘要。像这样:

$C$1
R=255, G=0, B=0
$C$2
R=255, G=255, B=0

更简单的方法是选择具有颜色的单元并按

格式化单元格>图案>颜色>自定义

enter image description here

然后RGB值可见。

关于条件格式,最好应用与条件相同的逻辑:Excel 2007 conditional formatting - how to get cell color?