Excel VBA fill down formulas

Sometimes, its easier to embed some formulas in an Excel spreadsheet than to automate it completely by VB, so I will show you how to fill down formulas using Excel VBA.

Here is the code to fill down a formula.  The arguments you pass to the function is the sheet name, the start column and end column of your columns that have functions in them that you would like to fill down.

Function fillDownFormulas(sheetName As String, startC As String, endC As String) As Boolean
  Dim WSD As Worksheet
  Set WSD = Worksheets(sheetName)
  ' Find the last row with data
  Dim finalRow As Long
  finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
  If finalRow <= 2 Then Exit Function
  ' Fill formulas down
  Dim sourceRange As Range
  Set sourceRange = WSD.Range(WSD.Cells(2, startC), WSD.Cells(2, endC))
  Dim fillRange As Range
  Set fillRange = WSD.Range(WSD.Cells(2, startC), WSD.Cells(finalRow, endC))
  sourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault
End Function

You can call the function like this:

 

fillDownFormulas("sheet1","G","I")
fillDownFormulas("sheet2","D","D")