Auto fill excel formula error

alang84

New Member
Joined
Oct 21, 2022
Messages
22
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I run into an issue when there aren't any rows of data after row 4. For example, the code works if data exists in rows 4 and 5, but will not work if data is only in row 4. I receive a debug error on the code in blue. Any thoughts?

Sub JF()
Application.ScreenUpdating = False

Sheets("Risk Factor Report").Select
Range("B4").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(RC[9]=""true"",RC[13]=""true"",RC[17]=""true"",RC[21]=""true""),""Review: ""&IF(RC[9]=""true"",""Issuer ""&IF(OR(RC[13]=""true"",RC[17]=""true""),""/ "",),)&IF(RC[13]=""true"",""Sector ""&IF(RC[17]=""true"",""/ "",),)&IF(RC[17]=""true"",""Industry""&IF(RC[21]=""true"",""/ "",),)&IF(RC[21]=""true"",""Country"",),""-"")"
Range("B5").Select

Dim lastRow As Long
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
With ActiveSheet
.Range("B4").AutoFill Destination:=.Range("B4:B" & lastRow)
End With

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Simple thing to check if lastRow is greater than 4, i.e.
VBA Code:
Dim lastRow As Long
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
If lastRow > 4 Then
    With ActiveSheet
        Range("B4").AutoFill Destination:=.Range("B4:B" & lastRow)
    End With
End If
 
Upvote 0
Solution
Simple thing to check if lastRow is greater than 4, i.e.
VBA Code:
Dim lastRow As Long
lastRow = Cells(Rows.Count, "C").End(xlUp).Row
If lastRow > 4 Then
    With ActiveSheet
        Range("B4").AutoFill Destination:=.Range("B4:B" & lastRow)
    End With
End If
This worked. Thanks a lot Joe4. Much appreciated.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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