VBA Selection.Autofil Dynamic Range for columns & rows

Busybeaver

New Member
Joined
Nov 26, 2010
Messages
20
Hi,

I've read a number of posts regarding autofill for dynamic ranges but they all refer to number of rows. Is there a way to also calculate a varying number of columns please? (PS: I love this site, I almost always find the help I need by reading through the threads!)

I have a dataset that will change number of both columns and rows. Under each column header is a formula. What I am trying to do is mimic the action of highlighting existing columns and autofilling the formulae down to the last row.

Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select

' This next part doesn't work because obviously it is only looking at column M and not all highlighted columns. Can I also make the column reference dynamic?
Selection.AutoFill Destination:=Range("M2:M" & Range("A" & Rows.Count).End(xlUp).Row)

Below is a sample of the sheet. Row 2 holds the formulae.

1686807193060.png
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try the following on a copy of your workbook - just change the sheet name to suit.
VBA Code:
Option Explicit
Sub Flexible_Autofill()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ *** Change sheet name to suit ***
    Dim LRow As Long, LCol As Long, i As Long
    LRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    LCol = ws.Cells(2, Columns.Count).End(xlToLeft).Column
    
    For i = 2 To LCol
        ws.Cells(2, i).AutoFill ws.Range(ws.Cells(2, i), ws.Cells(LRow, i))
    Next i
End Sub
 
Upvote 0
Dim ws As Worksheet Set ws = Worksheets("Sheet1") '<~~ *** Change sheet name to suit *** Dim LRow As Long, LCol As Long, i As Long LRow = ws.Cells(Rows.Count, "A").End(xlUp).Row LCol = ws.Cells(2, Columns.Count).End(xlToLeft).Column For i = 2 To LCol ws.Cells(2, i).AutoFill ws.Range(ws.Cells(2, i), ws.Cells(LRow, i)) Next i
Works Perfectly, thank-you so much :)
 
Upvote 0
Why not do all the columns at once instead of one-by-one?

VBA Code:
Sub Flexible_Autofill_v2()
  Dim ws As Worksheet
  Dim LRow As Long, LCol As Long
  
  Set ws = Worksheets("Sheet1")   '<~~ *** Change sheet name to suit ***
  LRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  LCol = ws.Cells(2, Columns.Count).End(xlToLeft).Column
  ws.Cells(2, 2).Resize(LRow - 1, LCol - 1).FillDown
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top