VBA发现正在查看隐藏的行

时间:2016-02-11 20:19:39

标签: vba excel-vba excel

我有下面的代码(循环)来搜索我的电子表格(D列)中的0,当它找到一个,如果执行复制/粘贴,然后删除该行。在所有过滤的0之后(列被A列过滤 - 重复)我告诉它结束sub。但是我发现find在过滤的隐藏行中找到了0,所以循环继续。

如何使查找仅对可见行有效,然后在处理完所有0后结束。

Set RangeObj = Cells.Find(What:="0", After:=ActiveCell, _
 LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
 SearchDirection:=xlNext, MatchCase:=False)

 If RangeObj Is Nothing Then RangeObj.Activate

 Cells.Find(What:="0", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
 xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
 , SearchFormat:=False).Activate

1 个答案:

答案 0 :(得分:0)

您需要的是SpecialCells(xlCellTypeVisible)方法和.FindNext方法。

见下面的代码:

Set RangeObj = Cells.SpecialCells(xlCellTypeVisible).Find(What:="0", After:=Range("A1"), _
                 LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False)

If Not RangeObj Is Nothing Then

    Dim sFirstAdd As String, sAdd As String
    sFirstAdd = RangeObj.Address

    Do

        sAdd = RangeObj.Address
        With RangeObj.EntireRow 'or limit to just the necessary columns
            .Copy 'choose your desired destination
            .Delete
        End With

        Set RangeObj = Cells.SpecialCells(xlCellTypeVisible).FindNext(After:=Range(sAdd))

    Loop Until RangeObj Is Nothing Or sAdd = sFirstAdd

End If