Making fill-down formula conditional

RaydenUK

Board Regular
Joined
Mar 25, 2014
Messages
74
I have been using the fill-down code below and it works perfect if there is more than 2 rows that it is filling down. However, sometimes I only have 1 or 2 rows and it ends up filling down to the last cell possible in excel. How can I make this formula conditional to correct this?


Code:
    Range("L4").Select
    ActiveCell.Offset(0, -1).Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, 1).Select
    Range(Selection, Selection.End(xlUp)).Select
    Selection.FillDown
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi RaydenUK,

Your code seems to flip flop between Columns "L" and "K", but in my testing it only Autofilled Column "L". That being said, let me know how this works for you. It is easily adjusted to include more columns and how many rows you actually want to Autofil.

Please be sure to test this code on a backup copy of your work. This code is going to write data to your sheet!

Code:
Sub autofil_What()

    Dim sht As Worksheet
    Dim LastRow As Long
    
    LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    Range("L4").AutoFill Range("L4:L" & LastRow)
    
End Sub

HTH

igold
 

RaydenUK

Board Regular
Joined
Mar 25, 2014
Messages
74
This almost works but I have borders around the entire page and it fills to the the last row with a border. How can I avoid this?
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Again, I am a little vague in exactly where the borders are, but see if this does what you are asking.

Code:
Sub af_What()

    Dim sht As Worksheet
    Dim LastRow As Long
    
    LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    Range("L4").AutoFill Range("L4:L" & LastRow)
    Range("L4:L" & LastRow).Borders.LineStyle = xlNone
    
End Sub

igold
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,564
Messages
5,596,870
Members
414,106
Latest member
Tigretto

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
Top