I have a nested If Else loop, which checks the date entered in a UserForm TextBox (date format and within the listed start and end dates).
Private Sub Setbutton_Click()
If Not IsDate(txtbox.Value) Then
MsgBox "Input is not in date format." & Chr(13) & "Enter PROJECT START date in date format." & Chr(13) & "(dd/mm/yyyy)", vbCritical
Cancel = True
Else
If CDate(txtbox.Value) < ThisWorkbook.Sheets("Home").Range("G4").Value Then
MsgBox "PROJECT START date is before SCHEDULE START date" & Chr(13) & "Enter a date after" & " " & ThisWorkbook.Sheets("Home").Range("G4") & " or press Cancel and modify the SCHEDULE START date.", vbCritical
Cancel = True
Else
If CDate(txtbox.Value) > ThisWorkbook.Sheets("Home").Range("G5").Value Then
MsgBox "PROJECT START date is after SCHEDULE END date" & Chr(13) & "Enter a date before" & " " & ThisWorkbook.Sheets("Home").Range("G5") & " or press Cancel and modify the SCHEDULE END date.", vbCritical
Cancel = True
Else
(If all okay run the Main code)
End If
End If
End If
Is there any way to simplify this?
Are you looking for ElseIf
?:
Private Sub Setbutton_Click()
If Not IsDate(txtbox.Value) Then
MsgBox "Input is not in date format." & Chr(13) & "Enter PROJECT START date in date format." & Chr(13) & "(dd/mm/yyyy)", vbCritical
Cancel = True
ElseIf CDate(txtbox.Value) < ThisWorkbook.Sheets("Home").Range("G4").Value Then
MsgBox "PROJECT START date is before SCHEDULE START date" & Chr(13) & "Enter a date after" & " " & ThisWorkbook.Sheets("Home").Range("G4") & " or press Cancel and modify the SCHEDULE START date.", vbCritical
Cancel = True
ElseIf CDate(txtbox.Value) > ThisWorkbook.Sheets("Home").Range("G5").Value Then
MsgBox "PROJECT START date is after SCHEDULE END date" & Chr(13) & "Enter a date before" & " " & ThisWorkbook.Sheets("Home").Range("G5") & " or press Cancel and modify the SCHEDULE END date.", vbCritical
Cancel = True
Else
' (If all okay run the Main code)
End If
End Sub