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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bearblackGlobal Program ManagerAuthor Commented:
Thanks for the explaination -- I have to admit though still a little beyond my skill level right now to understand.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.