PDA

View Full Version : Run code under specific criteria



Lartk
11-19-2012, 10:41 AM
How can I make it so this code only runs IF cell A9 has a date in it? If cell A9 is blank, then I would like to not run this code because an error occurs.

Sub BlockQuantityfidelity()
Const FORMULA_SUM As String = _
"=IF($G9="""","""",SUMPRODUCT(--($G$9:$G$<lastrow>=$G9),--($H$9:$H$<lastrow>=$H9),$AN$9:$AN$<lastrow>))"
Dim lastrow As Long

With ActiveSheet

lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("AC9").Resize(lastrow - 8).Formula = Replace(FORMULA_SUM, "<lastrow>", lastrow)
End With
End Sub

Jan Karel Pieterse
11-19-2012, 10:47 AM
Just insert:

If Not IsDate(Range("A1").Text) Then Exit Sub


directly after your second Dim statement.

Lartk
11-19-2012, 10:50 AM
Great, thank you!