VBA: Macro to Autofill

August 14th, 2004 | Categories: VBA | Tags: , ,

Take control of Autofill using vba with this simple macro code example. It takes the data from the range A1:A2 and autifills it to A20. This can be modified to autofill any way you would like using a little macro tweaking.

Selection1 is the range with the data to autofill.
Selection2 is the entire range to autofill, this includes the first range plus the blank spaces to autofill.

Public Sub MyAutoFill()
'by AutomateExcel.com

'Declare range Variables
Dim selection1 As Range
Dim selection2 As Range

'Set range variables = their respective ranges
Set selection1 = Sheet1.Range("A1:A2")
Set selection2 = Sheet1.Range("A1:A20")
'Autofill
selection1.AutoFill Destination:=selection2

End Sub

Here’s the before and after picture

No comments yet.