识别在VBA中具有颜色的列或行

时间:2018-11-17 10:28:16

标签: excel vba excel-vba

但是我有一个问题。是否可以使用VBA识别列/行是否包含颜色?例如,我有5000列的A到Z列。在第630行中,有几列用红色突出显示,我想在AA列中将其标记为“ RED”。另一个示例,在行2533的G列中,红色突出显示,并且在AA列中也应指示“ RED”。不包含任何颜色的行应标记为“白色”。在VBA中可以实现这种情况吗?

2 个答案:

答案 0 :(得分:1)

类似这样的东西:

注意,它将明智地检查行!!因此,如果您在G列中有颜色,但在A列中没有。这将在AA列中给您单词“ color”。我还选择检查背景色。不是特别是红色,因为有时人们会选择不同的“红色”。

如果只希望红色(RGB 255,0,0),请使用第二个IF语句并注释掉第一个。

Sub CheckColurRows()

Dim myRange As Range
Dim cl As Variant
Dim ws As Worksheet

Set ws = Worksheets("Sheet1")
Set myRange = ws.Range("A1:Z5000") 'Set range


For Each cl In myRange 'Loop through each cell in "myRange
    If cl.Interior.ColorIndex <> xlColorIndexNone Then 'Check if cell has any background colour (not only red)
    'If cl.Interior.Color = RGB(255, 0, 0) Then 'Check if cell only has red colour
        ws.Cells(cl.Row, 27) = "Colour" 'If cell has colour, then print the word "Colour" in Column 27 (Column AA)
    Else
        If ws.Cells(cl.Row, 27) <> "Colour" Then ws.Cells(cl.Row, 27) = "White" 'If cell already have the word colour in column AA, don't overwrite with "white"
    End If
Next cl

End Sub

结果将如下所示:

enter image description here

答案 1 :(得分:1)

Sub FFF()
    Dim rngRow As Range, cell As Range, red_color&
    red_color = RGB(255, 0, 0)
    For Each rngRow In Range("A1:Z5000").Rows
        For Each cell In rngRow.Cells
            cell = IIf(cell.Interior.Color = red_color, "RED", "WHITE")
        Next
    Next
End Sub
相关问题