Finding last row of a filtered range when only one row showing

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
Office Version
  1. 2019
I have the following code to get the first and last rows of a filtered range.
VBA Code:
With ActiveSheet.AutoFilter.Range
    Set rFiltered = .Resize(.Rows.Count - 1).Offset(1).Columns(2).SpecialCells(xlCellTypeVisible)
End With

FirstInvRow = rFiltered.Cells(1, 1).Row
LastInvRow = rFiltered.Cells(1, 1).End(xlDown).Row

The code works fine when there are 2 or more filtered rows showing, but if only one row is showing, LastInvRow is returned as 1048576.
How can I code for this contingency? I thought of simply checking IF LastInvRow=104576 then LastInvRow = FirstInvRow, but would there be occasions in the future when just one filtered row showing would return a different result than 104576? What is my best way of avoiding this problem?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
VBA Code:
LastInvRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
 
Upvote 0
Solution

Forum statistics

Threads
1,215,123
Messages
6,123,181
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top