VBA Excel: fill down formulas automatically

Hello experts, I found the following excellent procedure:

Sub FormulaFill()
    Dim cel As Range, rg As Range
    Dim lastrow As Long
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    If lastrow < 2 Then Exit Sub
    
    Application.ScreenUpdating = False
    
    Set rg = Range("A2:AZ" & lastrow)
    For Each cel In rg.Rows(1).Cells
        If cel.HasFormula Then
            cel.Copy
            Intersect(cel.EntireColumn, rg).PasteSpecial xlPasteFormulas
        End If
    Next
    Range("A1").Select

End Sub

Open in new window


I would like to adapt the code by adding the following requirement:

1-Be able to place a formula in the cell that I want ex: A5 and fill down the formula till the last row of a reported column.

Ex: I place a formula in A5.
When I launch the procedure I should have:

1-An input box asking "Select or report the range location of your formula?". Activecell by default.
2-An input box asking "Select or report your last column in which you want to paste your formula?", remark: I should be able to select the same column letter in which is located my formula.
3-The procedure should paste the formula as of the range of my formula to the reported column & last used row.

The idea is to add this procedure to my Personal.xlsb so we can use Activesheet as worksheet.

Thank you again for your help.
LVL 1
LD16Asked:
Who is Participating?

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

x
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I use this sub to paste the formulas:
Sub FormulaFill(Optional ByVal CellLocation As String)
    Dim cel As Range, rg As Range
    Dim lastrow As Long
    
    If CellLocation = "" Then
        lastrow = Range(Replace(ActiveCell.Address, Range(ActiveCell.Address).Row, "") & Rows.Count).End(xlUp).Row
        Set rg = Range(ActiveCell.Address, Replace(ActiveCell.Address, Range(ActiveCell.Address).Row, "") & lastrow)
    Else
        lastrow = Range(Replace(Range(CellLocation).Address, Range(CellLocation).Row, "") & Rows.Count).End(xlUp).Row
        Set rg = Range(CellLocation, Replace(Range(CellLocation).Address, Range(CellLocation).Row, "") & lastrow)
    End If
    
    If lastrow < 2 Then Exit Sub
    
    Application.ScreenUpdating = False
    
    
    If rg.Rows(1).Columns(1).HasFormula Then
        rg.Rows(1).Columns(1).Copy
        rg.PasteSpecial xlPasteFormulas
    End If
    
    Application.ScreenUpdating = True
    
End Sub

Open in new window

then idea is calling this sub when:

1. To specify a cell, use:
Call FormulaFill("A5")

Open in new window

2. To use active cell, use:
Call FormulaFill

Open in new window

LD16Author Commented:
Thank you for this proposal.

I tested your code by selecting my formula in C1, however I don't have the filldownformula applied for the formula.

2015-10-02-09_32_51-Microsoft-Excel-.png
Please find attached my test.

Thank you again for your  help.
FormulaFill_41020897.xlsm
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
based on the original scripts from your question, it refers to the lastrow of Column A, while my previous posted codes was looking for last row in same column....

if you wish to do the same, then you can try this instead.

in FormulaFill's module:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Rows.Count = 1 Then
        Call FormulaFill(Target)
    End If
End Sub

Sub FormulaFill(ByRef c As Range)
    Dim rg As Range
    Dim lastrow As Long
    
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    If lastrow < 2 Then Exit Sub
    
    Set rg = Range(c.Address, Replace(c.Address, c.Row, "") & lastrow)
    
    Application.ScreenUpdating = False
    
    If c.HasFormula Then
        c.Copy
        rg.PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
    End If
    
    Application.ScreenUpdating = True
End Sub

Open in new window

Pls note I rewrite the scripts so it should be cleaner.
FormulaFill_41020897_b.xlsm

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

LD16Author Commented:
Thank you it works perfectly.

I have a question, instead of using private sub as I want to add this in my PERSONAL.xlsb in order to call the macro in no matter worksheet that I want should I change Private Sub to Sub?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>I want to add this in my PERSONAL.xlsb in order to call the macro in no matter worksheet that I want should I change Private Sub to Sub?

You can declare it as Public Sub instead.
LD16Author Commented:
Pefect. Thank you for your help.
LD16Author Commented:
Thank you again for your help.
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.