Hi, I need a macro that will prompt the user to enter a start date in cell “O2” and then a end date in cell “O3” via a message box that will only except the date if entered in the format “dd mmm yyyy”. I think I have the basics below, but I want to add the following error handling:
- If entered in the incorrect format excel shows a runtime error. Instead I’d like a message box saying “retry entering start/end date in correct format”. Then returns to the input box that needs to be entered again. Loops until correct.
- Also, a check that the start date is before the end date would be great too.
Dim start_dt As Date
Dim end_dt As Date
start_dt = Application.InputBox("Enter start date in format: dd mmm yyyy")
end_dt = Application.InputBox("Enter end date in format: dd mmm yyyy")
Range("O2").Select
ActiveCell.Value = start_dt
MsgBox Format(start_dt, "dd mmm yyyy")
Range("O3").Select
ActiveCell.Value = end_dt
MsgBox Format(end_dt, "dd mmm yyyy")
End Sub
- If entered in the incorrect format excel shows a runtime error. Instead I’d like a message box saying “retry entering start/end date in correct format”. Then returns to the input box that needs to be entered again. Loops until correct.
- Also, a check that the start date is before the end date would be great too.
Dim start_dt As Date
Dim end_dt As Date
start_dt = Application.InputBox("Enter start date in format: dd mmm yyyy")
end_dt = Application.InputBox("Enter end date in format: dd mmm yyyy")
Range("O2").Select
ActiveCell.Value = start_dt
MsgBox Format(start_dt, "dd mmm yyyy")
Range("O3").Select
ActiveCell.Value = end_dt
MsgBox Format(end_dt, "dd mmm yyyy")
End Sub