Range.Rows property with autofilter applied

Joined
Nov 27, 2019
Messages
18
Office Version
  1. 365
  2. 2013
  3. 2010
Platform
  1. Windows
Hi MrExcel, I need your help.

I have an autofilter applied to a range resulting in non-continuous set of rows visible (see snapshot).
Snapshot_2.PNG


Assume I want to access the number of the first visible non-header row (which is number 7 in this case). When I run

VBA Code:
Dim rng As Range
Set rng = Ws.Range("B1:B100").SpecialCells(xlCellTypeVisible)
Debug.Print "XXX: " & rng.Address
Debug.Print "ZZZ: " & rng.Rows(2).row

, I get

Code:
XXX: $B$1,$B$7,$B$11,$B$25:$B$100
ZZZ: 2

which exactly matches my expectations in line XXX but does not in line ZZZ: I would expect rng.Rows(2) to return the ordinal row number (i.e., 7 and not 2).

What do I miss here? Please advise.

Thanks in advance,
Dmitry
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What do you get with

VBA Code:
Debug.Print "ZZZ: " & ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(12).Cells(1, 1).Row
 
Upvote 0
Maybe something like this...

VBA Code:
Sub aTest()
    Dim ws As Worksheet, rng As Range
      
    Set ws = Worksheets("Sheet1")
    With ws
        With .AutoFilter.Range.Columns(2)
            'This excludes header's row
            Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
        End With
        
        If Not rng Is Nothing Then
            MsgBox rng.Address
            MsgBox rng.Areas(1).Cells(1).Row
        End If
    End With
End Sub

Hope this helps

M.
 
Upvote 0
What do you get with

VBA Code:
Debug.Print "ZZZ: " & ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(12).Cells(1, 1).Row
Mark,

With this I get 7 as required! Impressive. May I ask if it's generalizable? I mean, what if I want to get the number of n-th visible row? I can't figure out how to increment the Offset() and Cells() arguments appropriately - I still get increments to 7 instead of 11.
 
Upvote 0
For a generalised approach I think you'll need to use a For Each loop and use a counter.
 
Upvote 0
You are welcome.
Remark: as Rory said to a generalized solution maybe you need to loop for each Area and for each Row inside the area.

M.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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