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
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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
 
Upvote 0
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?
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

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