【发布时间】:2017-11-03 07:13:11
【问题描述】:
我正在做一个宏来通过颜色索引检查和验证所有单元格。如果我的代码有错误,请帮助我。非常感谢。
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
【问题讨论】:
-
你的代码在哪里?
-
你得到错误还是什么?我可以告诉你,无论如何你只会运行 1 个单元格,因为你
Exit Sub在你的IF..ELSE的两个部分中。我会假设如果您输入Else语句就可以了,因此您不会退出子。 -
你不需要在你的'With语句中重复'cel'。我也认为您想删除两个“退出子”,但也许这就是您想要的?此外,您可以使用“For Each cel in rng”,因此无需 .Cells...
-
嗨。谢谢你。请立即查看我的代码。但我现在的问题是提示说黄色但我不再有黄色单元格,我担心它也会检测到隐藏的单元格。我怎样才能只合并活动单元格而忽略隐藏单元格的值。谢谢
-
在您的 If 语句中添加一个检查,以便
If .Interior.ColorIndex = 6 Then变为If .Interior.ColorIndex = 6 and not (.entirerow.Hidden or .entirecolumn.Hidden) Then
标签: excel validation loops colors vba