搜索表符合多个条件

时间:2013-02-06 17:22:53

标签: vba

For Each c1 In ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
If cl.Interior.ColorIndex = 16 Then

MsgBox "Error in " & c1.Address
Exit Sub ' To step out after first error
End If
Next
End Sub

我有这个代码在我的工作表中搜索颜色索引为16的非隐藏单元格。

但是,我希望添加第三个标准:SpecialCells(xlCellTypeBlanks)

这样只有在满足3个条件时才会显示该消息。

您的意见表示赞赏,

感谢

2 个答案:

答案 0 :(得分:1)

试试这个:If c1.Interior.ColorIndex = 16 And c1.Value2 = vbNullString Then

答案 1 :(得分:0)

首先,使用Option Explicit!这样,您就可以像在代码中一样阻止混淆clc1!此外,最佳做法是使用缩进来使代码更易于阅读。

您可以使用Application.Intersect

来实现您的目标
Option Explicit

Private Sub FindErrors()
    Dim c As Range
    For Each c In Application.Intersect( _
        ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible), _
        ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks))

        If c.Interior.ColorIndex = 16 Then
            c.Activate
            MsgBox "Error in " & c.Address
            Exit Sub
        End If
    Next c
End Sub
相关问题