Hi billkelley
If you are going to do the same thing twice in vba, it is worth trying to write a separate function for it that can be used both times. Here is my attempt:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Date < #12/2/2008# Then Exit Sub
With Sheets("Request for PTM")
' test if the first cell is empty here:
Cancel = StillEmpty(.Range("H11"), "Estimated Order Date Required")
'If cancelled, exit sub otherwise the user may get 2 irritating error messages together
If Cancel Then Exit Sub
' now test if the second cell is empty:
Cancel = StillEmpty(.Range("H12"), "Current Installed Irr. system entry required")
End With
End Sub
Private Function StillEmpty(r As Range, FailMsg As String) As Boolean
'Description : if r is empty, FailMsg is displayed and function returns true
' : otherwise, function returns false
If IsEmpty(r.Value) Then
StillEmpty = True 'if cell is empty, return true
r.Worksheet.Activate 'select r's worksheet
r.Select 'select empty cell
MsgBox FailMsg, , "Oops!"
End If
End Function
As the function StillEmpty returns true or false, you can assign it straight to the Cancel variable.
Bookmarks