2016-02-11 58 views
1

我有下面的代碼(循環)在我的電子表格(D列)中搜索0時,如果執行復制/粘貼,然後刪除行,它找到一個。在所有過濾後的0(該列由A列過濾 - 重複)後,我告訴它結束小組。但是我發現find在過濾的隱藏行中找到0,所以循環繼續進行。VBA查找正在查看隱藏行

如何使查找只在可見行上工作,然後在處理完所有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 

回答

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 
+0

我想你的代碼,現在設置RangeObj我得到一個類型不匹配:運行時錯誤13.用Google搜索它沒有找到一個解決方案。思考? – James

+0

@james - 現在試試。 –

+0

嘗試一下現在的斯科特嗎? – James