简体   繁体   English

vba FOR验证一定范围内的单元格颜色

[英]vba FOR to validate color of cells in a range

I am doing a macro to check and validate all cells by colorindex. 我正在做一个宏,以通过colorindex检查和验证所有单元格。 kindly help me if there is wrong in my code. 如果我的代码有误,请帮我。 Many Thanks. 非常感谢。

    Sub Validate()
Dim rng As Range: Set rng = Application.Range("CATALOG!B2:F98")
Dim cel As Range
Dim i As Boolean

i = False

For Each cel In rng
With cel
    If .Interior.ColorIndex = 6 Then
        If .EntireRow.Hidden = False Then
            i = True
        End If
    End If
   End With
   Next cel

 If i = True Then
  MsgBox "yellow"
Else
  MsgBox "none"
End If

End Sub

It's pretty unclear what you're asking, but there are two ways: 不清楚您要问什么,但是有两种方法:

1) first, when you want to check if every cell in range has .Interior.ColorIndex = 6 . 1)首先,当您要检查范围内的每个单元格是否具有.Interior.ColorIndex = 6 Then your code should look like: 然后您的代码应如下所示:

Sub Validate()
Dim rng As Range: Set rng = Application.Range("CATALOG!B2:F98")
Dim cel As Range
Dim i As Boolean

i = True
For Each cel In rng.Cells
    If Not cel.Interior.ColorIndex = 6 Then
        i = False
        Exit For 'we found cell with different color, so we can exit loop
    End If
Next cel

If i = True Then
    MsgBox "yellow"
Else
    MsgBox "none"
End If

End Sub

2) you want to check if any cell has .Interior.ColorIndex = 6 . 2)您要检查是否有任何单元格具有.Interior.ColorIndex = 6 Then it should look like: 然后应该看起来像:

Sub Validate()
Dim rng As Range: Set rng = Application.Range("CATALOG!B2:F98")
Dim cel As Range
Dim i As Boolean

i = False
For Each cel In rng.Cells
    If cel.Interior.ColorIndex = 6 Then
        i = True
        Exit For 'we found cell with the color, so we can exit loop
    End If
Next cel

If i = True Then
    MsgBox "yellow"
Else
    MsgBox "none"
End If

End Sub

Of what I've gatered you are trying to loop through all non hidden cells and see if they have a yellow background color, and if they are yellow they need to change based on a input. 在我介绍的内容中,您试图遍历所有非隐藏单元格,以查看它们是否具有黄色背景色,如果它们是黄色,则需要根据输入进行更改。

Sub Validate()

Dim rng As Range: Set rng = Application.Range("CATALOG!B1:F98")
Dim cel As Range

For Each cel In rng
    With cel
        If .Interior.ColorIndex = 6 Then
            If .EntireRow.Hidden = False Then
                .Interior.ColorIndex = InputBox("please input new colorIndex", "SetColorIndex")
            End If
        End If
   End With
Next cel

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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