6

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

4

6 回答 6

4

过滤后,对 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
...
于 2012-09-25T16:31:32.467 回答
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
于 2012-09-25T16:17:26.690 回答
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
于 2021-05-14T07:47:19.757 回答
0

经过大量研究,提出了不同的选择,我将其中一些放在一起,这对我来说似乎工作正常(我让它在表格中工作):

希望你觉得它有用。

ActiveSheet.ListObjects("Table").Range.SpecialCells(xlCellTypeVisible).Select

b = Split(Selection.Address, "$")
iRes = UBound(b, 1)
If iRes = -1 Then
    iRes = 0
End If

LastRow = Val(b(iRes))
于 2022-03-03T21:36:22.407 回答
-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
于 2016-04-27T09:14:58.610 回答
-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
        ...
于 2014-04-15T11:15:42.430 回答