2012-09-25 104 views
4

如何在篩選工作表中的數據時查找最後一行數據?我一直在玩Special CellsVisible Cells,但找不到解決方案。我想這一定是什麼我有以下某種變化:從篩選範圍獲取最後一行

... 
    With ws 
     LR = .Range("A" & Rows.Count).End(xlUp).Row 
     .Range("A1:E" & LR).AutoFilter Field:=2, Criteria1:="=4" 
     LRfilt = .Range("A" & Rows.SpecialCells(xlCellTypeVisible).Count).End(xlUp).Row 
     Debug.Print LR 
     Debug.Print LRfilt 
    End With 
    ... 

文件可以在這裏找到:

wikisend.com/download/443370/FindLRFilteredData.xls

編輯:

與Siddharth討論後意識到我不希望我需要的Last Row屬性找到導致Sid解決方案的可見行數...

回答

2

編輯:郵政聊天跟進

Option Explicit 

Sub FilterTest() 
    Dim rRange As Range, fltrdRng As Range, aCell As Range, rngToCopy As Range 
    Dim ws As Worksheet 
    Dim LR As Long 

    '~~> Change this to the relevant sheet 
    For Each ws In ThisWorkbook.Worksheets 
     If Not ws.Name = "Sheet1" Then 
      With ws      
       '~~> Remove any filters 
       .AutoFilterMode = False 

       LR = .Range("A" & Rows.Count).End(xlUp).Row 

       '~~> Change this to the relevant range 
       Set rRange = .Range("A1:E" & LR) 

       With rRange 
        '~~> Some Filter. Change as applicable 
        .AutoFilter Field:=2, Criteria1:=">10" 

        '~~> Get the filtered range 
        Set fltrdRng = .SpecialCells(xlCellTypeVisible) 
       End With 

       For Each aCell In fltrdRng 
        If aCell.Column = 1 Then 
         If rngToCopy Is Nothing Then 
          Set rngToCopy = aCell 
         Else 
          Set rngToCopy = Union(rngToCopy, aCell) 
         End If 
        End If 
       Next 

       Debug.Print ws.Name 
       Debug.Print rngToCopy.Address 

       'rngToCopy.Copy 

       Set rngToCopy = Nothing 

       '~~> Remove any filters 
       .AutoFilterMode = False 
      End With 
     End If 
    Next 
End Sub 
+0

嗯,這將產生與上述方法相同的結果。我正在使用多個工作表的循環,但用單張紙進行測試並得到相同的結果。不知道發生了什麼,我可以給你發送文件嗎? –

+0

是的。您可以在www.wikisend.com上傳文件並在您的問題中分享鏈接。 –

+0

http://wikisend.com/download/443370/FindLRFilteredData.xls –

3

過濾後,用同樣的公式爲LASTROW將返回最後過濾行:

... 
With ws 
    LR = .Range("A" & Rows.Count).End(xlUp).Row 
    .Range("A1:E" & LR).AutoFilter Field:=2, Criteria1:="=4" 
    LRfilt = .Range("A" & Rows.Count).End(xlUp).Row 
    Debug.Print LR 
    Debug.Print LRfilt 
End With 
... 
+0

+ 1非常真實。刪除我的答案。我忘了這個... –

+0

確定我已經測試過了!必須試圖讓事情複雜化。 –

+1

好吧,現在正在測試這回,它不工作。 'LRFilt'仍然和'LR'一樣。非常困惑,因爲我在家裏使用了「Excel 2007」,唯一的區別是在家裏有'W7'和辦公室裏有'XP'。 @Siddharth你能否取消你的回答? –

-2

這是最簡單的解決方案

... 
     With ws 
      .Range("A1:E1").AutoFilter Field:=2, Criteria1:="=4" 
      LRfilt=.Range("A1", .Range("A1").End(xlDown)).End(xlDown).Row 
      Debug.Print LRfilt 
     End With 
     ... 
+0

這是不正確的,因爲修正的問題和堅果的反正是完全相同的答案 –

0

這似乎工作。當過濾器處於正常狀態時,.end(xlUp)給出過濾範圍的最後一行,但不是表單的最後一行。我建議你使用這種技術來得到最後一行:

Sub GetLastRow 
    ' Find last row regardless of filter 
    If Not (ActiveSheet.AutoFilterMode) Then  ' see if filtering is on if already on don't turn it on  
     Rows(1).Select       ' Select top row to filter on 
     Selection.AutoFilter      ' Turn on filtering 
    End if   
    b = Split(ActiveSheet.AutoFilter.Range.Address, "$") ' Split the Address range into an array based on "$" as a delimiter. The address would yeild something like $A$1:$H$100 
    LastRow= Val(b(4)) ' The last value of the array will be "100" so find the value 
End sub