Excel

Automatically Fill-Down Formulas for Non-Adjecent Columns

Ease of Use

Easy

Version tested with

2002, 2003 

Submitted by:

Jacob Hilderbrand

Description:

This macro will automatically fill down a formula in a specified cell for the number of rows based on a specified column. 

Discussion:

This macro will allow you to use the Auto-Fill feature of Excel, but you will not have to drag the cell (to be filled) down for an indeterminate number of rows. Suppose your formula is in E1 and you need to fill this formula down for all the rows used in Column A. With this macro, just specify the data range (A:A) and the fill range (E1) and the macro will do the rest. 

Code:

instructions for use

			

Option Explicit Sub AutoFillMacro() Dim LastRow As Long Dim FillRange As Range Dim DataRange As Range Dim Prompt As String Dim Title As String Prompt = "Select the column that contains your data." Title = "Data Range Input" On Error Resume Next Set DataRange = Application.InputBox(Prompt, Title, _ ActiveCell.EntireColumn.Address, , , , , 8) On Error GoTo 0 If DataRange Is Nothing Then Exit Sub End If Prompt = "Select the cell with the formula to be used for the fill." Title = "Fill Range Input" On Error Resume Next Set FillRange = Application.InputBox(Prompt, Title, _ ActiveCell.Address, , , , , 8) On Error GoTo 0 If FillRange Is Nothing Then Exit Sub End If Set FillRange = FillRange(1, 1) LastRow = Cells(65536, DataRange.Column).End(xlUp).Row If LastRow > FillRange.Row Then FillRange.AutoFill Range(FillRange.Address & ":" & _ Cells(LastRow, FillRange.Column).Address), xlFillDefault End If End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code from above in the Code Window that opens up.
  5. Close the VBE (Alt + Q or press the X in the top-right corner).
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select AutoFillMacro and press Run.
 

Sample File:

AutoFill.zip 7.14KB 

Approved by mdmackillop


This entry has been viewed 222 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express