从过滤范围获取最后一行

Ali*_*eir 6 excel vba autofilter excel-vba

如何过滤工作表中的数据,如何找到最后一行数据?我一直在玩Special Cells,Visible 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
    ...
Run Code Online (Sandbox Code Playgroud)

文件可以在这里找到:

wikisend.com/download/443370/FindLRFilteredData.xls

编辑:

在与Siddharth讨论后意识到我不希望Last Row我需要的属性能够找到导致Sid解决方案的可见行数的计数......

Sid*_*out 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
Run Code Online (Sandbox Code Playgroud)