Find Row Number for 25th Visible Row

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
980
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I am trying to figure out a bit of code which will fit into a much larger module.

Basically I have an Excel Table, for which the Header Row is in row 6. The table may or may not be filtered.

I want to be able to find the row number of the 25th visible row (even if it is blank and outside of my table range). Step one in my troubleshooting is to just select the range. The code below works, but it also selects hidden rows as part of the '25'.

I don't wish to step through each row in a loop checking the Hidden property of each cell and doing a count if possible.

Any ideas where I'm going wrong?

Code:
Sub Find_25th_Row()
    Dim myUniqueCells As Range
    Set myUniqueCells = Sheets(“Sheet1").AutoFilter.<wbr>Range.Offset(1, 0).Resize(25, 1).Cells.SpecialCells(<wbr>xlCellTypeVisible)
    myUniqueCells.Select
End Sub

Help/Guidance is much appreciated
Thanks
Caleeco
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If "Excel Table" means a list object, maybe one of those list object quirks. Try this after you replace the bold red font with the name of your table.

Rich (BB code):
 Set myUniqueCells = Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.Resize(25, 1).SpecialCells(xlCellTypeVisible)
 myUniqueCells.Select
 
Upvote 0
This does loop through Areas, but its better than looping through cells.

Code:
Function NthVisibleRow(N As Long)
    Dim VisCells As Range, oneArea As Range
    Dim N As Long
    
    With ActiveSheet
        Set VisCells = .Cells.SpecialCells(xlCellTypeVisible)
    End With
    For Each oneArea In VisCells.Areas
        If oneArea.Rows.Count >= N Then
            NthVisibleRow = oneArea.Cells(N, 1).Row
            Exit Function
        Else
            N = N - oneArea.Rows.Count
        End If
    Next oneArea
End Function
 
Last edited:
Upvote 0
If "Excel Table" means a list object, maybe one of those list object quirks. Try this after you replace the bold red font with the name of your table.

Rich (BB code):
 Set myUniqueCells = Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.Resize(25, 1).SpecialCells(xlCellTypeVisible)
 myUniqueCells.Select

Hi Joe,

Thanks for the reply. Yes, I did mean list object! Sorry for not being clearer.

I added the reference to the LO as suggested, the result is the same unfortunately. Im not sure why the SpecialCells(xlCellTypeVisible) property won't exclude filtered rows within the table. Very confusing!

Is there anything else I could try? Been pulling my hair out for a couple hours over it :laugh:

Kind Regards
Caleeco
 
Upvote 0
This does loop through Areas, but its better than looping through cells.

Code:
Function NthVisibleRow(N As Long)
    Dim VisCells As Range, oneArea As Range
    Dim N As Long
    
    With ActiveSheet
        Set VisCells = .Cells.SpecialCells(xlCellTypeVisible)
    End With
    For Each oneArea In VisCells.Areas
        If oneArea.Rows.Count >= N Then
            NthVisibleRow = oneArea.Cells(N, 1).Row
            Exit Function
        Else
            N = N - oneArea.Rows.Count
        End If
    Next oneArea
End Function

Thanks for the reply Mike. The code above seems to count the blank cells above the table (Headers are in row 6). Please see image below. I know I could counter this by just adding 6 to the final total, but that doesn't seem to work in all scenarios.

2q0w1sg.jpg


Thanks for your help thus far
Kind Regards

Caleeco
 
Upvote 0
That returns the row number of the nth visible row on the sheet.
If you want the row number of the nth visible row in some range, change this part to match your needs.
Code:
With ActiveSheet
    Set VisCells = .Cells.SpecialCells(xlCellTypeVisible)
End With
so that VisCells starts on the first row that you consider significant.

Alternatly, if you want it to find the 25th visible row, ignoring the always visible first 6 rows, then change N to 31
 
Last edited:
Upvote 0
Don't know if this helps, but it locates the 25th visible row below the table header row. Assumes hidden rows, if any, are all within the Table.
Code:
Sub VisibleRow25()
Dim FirstTableRow As Long, LastTableRow As Long, VisTableRows As Long, LastVisTableRow As Long, VisRow25 As Long
Dim myUniqueCells As Range
'The first (header) and last rows of the table are fixed regardless of the filter state of the Table
FirstTableRow = Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.Cells(1, 1).Row
LastTableRow = FirstTableRow + Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.Rows.Count - 1
Set myUniqueCells = Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.SpecialCells(xlCellTypeVisible)
For i = 1 To myUniqueCells.Areas.Count
    VisTableRows = VisTableRows + myUniqueCells.Areas(i).Rows.Count
Next i
'Next variable not needed, but locates the last Table row that's visible
LastVisTableRow = Split(Replace(myUniqueCells.Areas(myUniqueCells.Areas.Count).Address, ":", ""), "$")(4)
If VisTableRows >= 25 Then
    VisRow25 = FirstTableRow + 25
Else
    VisRow25 = LastTableRow + 1 + 25 - VisTableRows
End If
MsgBox VisRow25
End Sub
 
Upvote 0
That returns the row number of the nth visible row on the sheet.
If you want the row number of the nth visible row in some range, change this part to match your needs.
Code:
With ActiveSheet
    Set VisCells = .Cells.SpecialCells(xlCellTypeVisible)
End With
so that VisCells starts on the first row that you consider significant.

Alternatly, if you want it to find the 25th visible row, ignoring the always visible first 6 rows, then change N to 31

Don't know if this helps, but it locates the 25th visible row below the table header row. Assumes hidden rows, if any, are all within the Table.
Rich (BB code):
Sub VisibleRow25()
Dim FirstTableRow As Long, LastTableRow As Long, VisTableRows As Long, LastVisTableRow As Long, VisRow25 As Long
Dim myUniqueCells As Range
'The first (header) and last rows of the table are fixed regardless of the filter state of the Table
FirstTableRow = Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.Cells(1, 1).Row
LastTableRow = FirstTableRow + Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.Rows.Count - 1
Set myUniqueCells = Sheets("Sheet1").ListObjects("Table1").AutoFilter.Range.SpecialCells(xlCellTypeVisible)
For i = 1 To myUniqueCells.Areas.Count
    VisTableRows = VisTableRows + myUniqueCells.Areas(i).Rows.Count
Next i
'Next variable not needed, but locates the last Table row that's visible
LastVisTableRow = Split(Replace(myUniqueCells.Areas(myUniqueCells.Areas.Count).Address, ":", ""), "$")(4)
If VisTableRows >= 25 Then
    VisRow25 = FirstTableRow + 25
Else
    VisRow25 = LastTableRow + 1 + 25 - VisTableRows
End If
MsgBox VisRow25
End Sub

Thanks for the replies gents. I got it working using a combination of your suggestions, with a slight modification. I did it at work so don't have the code to hand, but the pseudo-code was something like this:

1. Using the Listobjects("Table1").DataBodyRange, COUNT how many of it's rows were visible (VisCount)
2. COUNT how many ListRows there were in the list object (regardless if they were hidden or not)

Code:
IF VisCount  >=25 THEN
       'use the code @[B][URL="https://www.mrexcel.com/forum/members/mikerickson.html"]mikerickson[/URL][/B] wrote (area loop)
ELSE 
       Nth row = 25 - VisCount + Listrows.count + 6
End IF

That seems to work pretty well, and expands beyond the list object range!

So thank you both for the input, may not have been able to do it without your help!

EDIT: @JoeMoe Just read your method, looks like we had a similar solution! ;)

Kind Regards
Caleeco:biggrin:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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