2012-11-30 56 views
1

如何存儲和檢索使用VBA從AutoFilter操作返回的行號?例如,我使用@brettdj代碼from this question(請參閱下面的代碼)刪除B列下所有具有「X」的行。現在我需要用X存儲行號(B4,B6,B9 - 請參閱下面的屏幕截圖),因爲我需要刪除同一工作簿中其他工作表上的相同行。使用VBA存儲自動篩選行號

Before AutoFilterAfter AutoFilter

Sub QuickCull() 
    Dim ws As Worksheet 
    Dim rng1 As Range 
    Set ws = Sheets("Sheet1") 
    Set rng1 = ws.Range(ws.[b2], ws.Cells(Rows.Count, "B").End(xlUp)) 
    Application.ScreenUpdating = False 
    With ActiveSheet 
      .AutoFilterMode = False 
      rng1.AutoFilter Field:=1, Criteria1:="X" 
      rng1.Offset(1, 0).EntireRow.Delete 
      .AutoFilterMode = False 
     End With 
    Application.ScreenUpdating = True 
End Sub 
+1

應用過濾器後,通過單元格向後*步* 'rng1':如果'.RowHeight'> 0,那麼刪除其他表中的那一行。 –

回答

3

Is it possible to fill an array with row numbers which match a certain criteria without looping?使用代碼,你可以不AutoFilter

例如快速返回這些行,該代碼將返回其中X內B2:B50000

發現了一個行範圍
Sub GetEm() 
Dim StrRng As String 
StrRng = Join(Filter(Application.Transpose(Application.Evaluate("=IF(B2:B50000=""X"",""B""&ROW(B2:B50000),""X"")")), "X", False), ",") 
If Len(StrRng) > 0 Then MsgBox Range(StrRng).EntireRow.Address & " could be deleted elsewhere" 
End Sub