Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select first visible cell directly beneath the header of a filtered column

Tags:

excel

vba

I am trying to select the first visible cell directly beneath the header of a filtered column. The code I am getting is as below, but I have to problems with this code. First, the first line of code is using the current active range of the file. It is highly likely that this file will change and this range will not be the same. How can I make it work for any file I would use it on? Second, if I use a totally different file with the same column format, the first visible cell under Column J could be J210. How can I make this work for any array of variables?

Sub Macro16()
'
' Macro16 Macro
'

'
    ActiveSheet.Range("$A$1:$R$58418").AutoFilter Field:=12, Criteria1:= _
        "Sheets"
    Range("J2").Select
    ActiveCell.FormulaR1C1 = "=RIGHT(RC[1],3)"
    Selection.FillDown
End Sub
like image 933
Todd Avatar asked Oct 27 '15 19:10

Todd


2 Answers

Sub FirstVisibleCell()
    With Worksheets("You Sheet Name").AutoFilter.Range
       Range("A" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
    End With
End Sub
like image 68
Orin Moyer Avatar answered Nov 15 '22 07:11

Orin Moyer


Untested but:

Sub Macro16()

    With ActiveSheet.Range("A1").CurrentRegion
        .AutoFilter field:=12, Criteria1:="Sheets"
        If .Columns(1).SpecialCells(xlCellTypeVisible).count > 1 Then
            With .Columns(10)
                .Resize(.rows.count - 1).offset(1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RIGHT(RC[1],3)"
            End With
        End If
    End With

End Sub
like image 27
Rory Avatar answered Nov 15 '22 07:11

Rory