Autofill down according to adjacent column

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Can someone please explain why my code doesn't work?
I want the VBA module to automatically fill certain cells (F,S,..,CS) and their corresponding formulas depending on whether cell in the column left of them is blank or not.


Code:
Sub test()

For Each sh In ActiveWorkbook.Sheets
If sh.Name <> "Overview" Then
Dim lastRowC1 As Long
Dim lastRowC2 As Long
Dim lastRowC3 As Long
Dim lastRowC4 As Long
Dim lastRowP1 As Long
Dim lastRowP2 As Long
Dim lastRowP3 As Long
Dim lastRowP4 As Long
    lastRowC1 = Range("E" & Rows.Count).End(xlUp).Row
    Range("D2").AutoFill Destination:=Range("D2:D" & lastRowC1)
    lastRowC2 = Range("R" & Rows.Count).End(xlUp).Row
[B]    Range("S3").AutoFill Destination:=Range("S3:S" & lastRowC2)[/B]
    lastRowC3 = Range("AE" & Rows.Count).End(xlUp).Row
    Range("AF3").AutoFill Destination:=Range("AF3:AF" & lastRowC3)
    lastRowC4 = Range("AR" & Rows.Count).End(xlUp).Row
    Range("S3").AutoFill Destination:=Range("AS3:AS" & lastRowC4)
    lastRowP1 = Range("BE" & Rows.Count).End(xlUp).Row
    Range("BF3").AutoFill Destination:=Range("BF3:BF" & lastRowP1)
    lastRowP2 = Range("BR" & Rows.Count).End(xlUp).Row
    Range("BS3").AutoFill Destination:=Range("BS3:BS" & lastRowP2)
    lastRowP3 = Range("CE" & Rows.Count).End(xlUp).Row
    Range("CF3").AutoFill Destination:=Range("CF3:CF" & lastRowP3)
    lastRowP4 = Range("CR" & Rows.Count).End(xlUp).Row
    Range("CS3").AutoFill Destination:=Range("CS3:CS" & lastRowP4)
    End If
    Next sh
    
End Sub

The bold line of code is an error according to Excel. Anyone any idea why this line generates an error
(and the line two rows earlier doesn't?)
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
the line you had embolened did not fail when I ran the code but this line did
Code:
Range("S3").AutoFill Destination:=Range("AS3:AS" & lastRowC4)
and the reason it did is because cell S3 is not in the auto fill range. I believe you have a typo in your code and the S3 should be AS3. Try
Code:
Range("[COLOR=#B22222]A[/COLOR]S3").AutoFill Destination:=Range("AS3:AS" & lastRowC4)
and see if it then works.
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

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