Autofilter Filldown in Excel

I need to use an autofilter and then update columns using the filldown function. The problem is that I don't know what the first row will be. Is there a way to determine the first visible row in a column within a named range and then insert a formula into that cell.
Selection.AutoFilter Field:=12, Criteria1:="*Comp*"
    Selection.AutoFilter Field:=17, Criteria1:="*Exp*"
    Application.Goto Reference:="Filter_Total"
    If Selection <> 0 Then
        Range("P7").FormulaR1C1 = "=EOMONTH(Now(),0)"
        Range([P7], Cells(Rng.Row, "P")).FillDown
    End If

Open in new window

bearblackGlobal Program ManagerAsked:
Who is Participating?

The SpecialCells method can be set to return just the visible cells from a range (ie those still visible after an autofilter) so this is the thing to use.  Also you don't actually need to use FillDown as once you have those visible cells, you can just assign the formula to them.  If you replace all the code you had above with the following, it should work just fine:

Dim rngAutofilter As Range, rngAfterFilter As Range
With Selection
    .AutoFilter Field:=12, Criteria1:="*Comp*"
    .AutoFilter Field:=17, Criteria1:="*Exp*"
    'next bit assigns a reference to the autofilter range in column P (includes header row)
    Set rngAutofilter = Application.Intersect(.Parent.AutoFilter.Range, .Parent.Range("P:P"))
    'next bit removes header row from rngAutoFilter
    With rngAutofilter
        Set rngAutofilter = .Resize(.Rows.Count - 1).Offset(1)
    End With
    'next bit checks to see if you do have any cells in column P that are visible after the filter has been applied:
    On Error Resume Next  'needed to avoid error if none visible!
    Set rngAfterFilter = rngAutofilter.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    'now check that cells returned:
    If Not rngAfterFilter Is Nothing Then
        rngAfterFilter.FormulaR1C1 = "=EOMONTH(Now(),0)"
    End If
End With

Open in new window

bearblackGlobal Program ManagerAuthor Commented:
Thanks for the explaination -- I have to admit though still a little beyond my skill level right now to understand.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.