Alistair Weir
Alistair Weir

Reputation: 1849

Get Last Row From Filtered Range

How do you find the last row of data when the data in your worksheet is filtered? I have been playing around with Special Cells and Visible Cells but cannot find a solution. I think it must be some kind of variation on what I have below:

    ...
    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
    ...

File can be found here:

wikisend.com/download/443370/FindLRFilteredData.xls

Edit:

Realised after discussion with Siddharth I did not want the Last Row property I needed to find a count of the number of visible rows which led on to Sid's solution below...

Upvotes: 6

Views: 50276

Answers (6)

Gustavo Arreola
Gustavo Arreola

Reputation: 1

After a lot of researching, came up with different options and I put some of them together which seems to be working fine for me (I made it work in a Table):

Hope you find it useful.

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))

Upvotes: 0

GvMLux
GvMLux

Reputation: 1

Assuming your data is already filtered, you can try this:

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

Upvotes: 0

Peter M-B
Peter M-B

Reputation: 1

This seems to work. When filters are on the normal .end(xlUp) gives the last row of a filtered range, but not the last row of the sheet. I suggest you use this technique to get the last row:

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

Upvotes: -1

user3466047
user3466047

Reputation: 9

This is simplest solution

...
        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
        ...

Upvotes: -3

Siddharth Rout
Siddharth Rout

Reputation: 149305

EDIT: Post Chat Followup

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

Upvotes: 1

nutsch
nutsch

Reputation: 5962

After the filter, using the same formula for the lastrow will return the last filtered row:

...
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
...

Upvotes: 4

Related Questions