【问题标题】:VBA find is looking at hidden rowsVBA find 正在查看隐藏的行
【发布时间】:2016-02-11 20:19:39
【问题描述】:

如果执行复制/粘贴然后删除该行,我有下面的代码(循环)在我的电子表格(D 列)中搜索 0。在所有过滤的 0 之后(该列被 A 列过滤 - 重复)我告诉它结束 sub。但我发现发现是在过滤的隐藏行中找到 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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    你需要的是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
    

    【讨论】:

    • 我试过你的代码,现在在 Set RangeObj 上我得到一个类型不匹配:运行时错误 13。谷歌搜索它找不到解决方案。想法?
    • @james - 抱歉,我编辑了帖子。现在试试。如果仍然出现错误,请告诉我是哪一行。
    • 你改变了什么?
    • @James - 我通过使用sAdd 变量更改了在删除找到的行后检查RangeObj 的方式。
    • 还是一样;设置 RangeObj = Cells.SpecialCells(xlCellTypeVisible).Find(What:="0", After:=ActiveCell, _ LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False)
    猜你喜欢
    • 1970-01-01
    • 2023-02-14
    • 2020-12-14
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    相关资源
    最近更新 更多