# Auto fill excel formula error



## alang84 (Dec 19, 2022)

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


----------



## Joe4 (Dec 19, 2022)

Simple thing to check if lastRow is greater than 4, i.e.

```
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
```


----------



## alang84 (Dec 19, 2022)

Joe4 said:


> Simple thing to check if lastRow is greater than 4, i.e.
> 
> ```
> Dim lastRow As Long
> ...


This worked.  Thanks a lot Joe4.  Much appreciated.


----------



## Joe4 (Dec 19, 2022)

You are welcome.
Glad I was able to help!


----------

