Best way to determine lastrow - AFTER executing a Filter (1 Viewer)

Isaac

Lifelong Learner
Local time
Today, 14:58
Joined
Mar 14, 2017
Messages
8,777
Code:
Function ExistsInRCP(str1ID As String, str2Id As String) As Boolean
Dim ws As Worksheet, lastrow As Long, rng As Range
Set ws = ThisWorkbook.Worksheets("RCP")
lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.ListObjects("ListObjectName").Range.AutoFilter _
    Field:=3, Criteria1:= _
    str1ID & "_" & str2Id

Note how I get the lastrow in the sheet, a common method. (I've also seen some other methods, utilizing UsedRange or SpecialCells, but have been uncertain as to their advantages/disadvantages).

Let's say I execute that code and it applies a filter. Now, I want to find out whether "some rows" or "zero rows" were left, after applying the filter. It will be very possible when this function is called, that zero rows are returned.

What do you think is the BEST way to see if zero rows are returned? Just to use lastrow method again, and see if it goes all the way up to row 1?
Is there maybe another way I don't know that is better.

By the way, this is a table that was created by making and running a Data Connection to a Sharepoint list.
 

Isaac

Lifelong Learner
Local time
Today, 14:58
Joined
Mar 14, 2017
Messages
8,777
Well, I can partly answer my own question with more experimentation now done.

the "lastrow" method (xlup) isn't accurate once the autofilter has been applied. if the autofilter is applied, and results in ZERO rows, then going to cell 1 million and doing an xlUp will first take you to the bottom row of the original range/table ....and only if you arrow up once more are you then in row 1. But I suppose I can work with that.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:58
Joined
Aug 30, 2003
Messages
36,125
Shot in the dark because I've never used Excel filtering, but does this method from one of my apps work?

lngLastRow = xl.Cells.SpecialCells(11).Row
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:58
Joined
Aug 30, 2003
Messages
36,125
Geez, I guess I took a long time to find that. Sorry!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:58
Joined
Aug 30, 2003
Messages
36,125
That does seem to work, thanks Paul, this is very helpful!

Glad it helped Isaac. I use that when I'm pushing data into Excel with automation and I want to add totals or whatever at the bottom.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:58
Joined
Sep 21, 2011
Messages
14,265
For my benefit please?, what does the 11 represent?
 

Users who are viewing this thread

Top Bottom