VBA – Autofill with a Macro


Autofill in VBA

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

tiphacks4

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!


<<Return to VBA Examples

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)