I would like to define the variable of the 3rd visible cell in my filtered column B.
The position of my 3rd visible cell can change according to the value selected in my filter.
In my example, the 3rd cell is B16, but it is not always this value. If I manage to define the variable of my 3rd visible cell in column B, I would be able to change B16 in my VBA code by this variable.
Sub jfdjdgfjg() Dim LastRow As Long LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row ActiveCell.Formula = "= SUM(B16:B" & LastRow & ")" 'Sum until lastrow End Sub
This is one method
Option Explicit Sub thirdviscell() Dim LRow As Long, i As Long, counter As Long Dim thirdcell As Range With ThisWorkbook.Sheets(1) LRow = .Cells(.Rows.Count, "A").End(xlUp).Row For i = 2 To LRow 'assuming a header row not to be counted If .Rows(i).EntireRow.Hidden = False Then counter = counter + 1 If counter = 3 Then Set thirdcell = .Cells(i, "B") Exit For End If Next i End With MsgBox "The third visible cell's address is " & thirdcell.Address(0,0), vbInformation End Sub
You can loop over the visible cells and count them like this:
Option Explicit Private Sub Example() MsgBox GetFilteredRowNumber(ActiveSheet.Range("B:B"), 3) End Sub Private Function GetFilteredRowNumber(ByRef r As Range, ByRef num As Long) As Long Dim aCell As Range, i As Long GetFilteredRowNumber = 0 For Each aCell In Intersect(r.Parent.UsedRange, r.SpecialCells(xlCellTypeVisible)).Cells i = i + 1 If i = num Then GetFilteredRowNumber = aCell.Row Exit For End If Next aCell End Function