从过滤范围获取最后一行

时间:2012-09-25 16:06:02

标签: excel vba excel-vba autofilter

如果过滤工作表中的数据,如何找到最后一行数据?我一直在使用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解决方案的可见行数...

5 个答案:

答案 0 :(得分: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
...

答案 1 :(得分:1)

编辑:发布聊天后续

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

答案 2 :(得分:0)

假设你的数据已经被过滤了,你可以试试这个:

Range("A1").Select

Dim FinalRowFiltered as Long

Dim FR as as String

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

FR = "A" & CStr(FinalRowFiltered)

Range(FR).Select

答案 3 :(得分:-1)

这似乎有效。当过滤器处于正常状态时.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

答案 4 :(得分:-3)

这是最简单的解决方案

...
        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
        ...
相关问题