error checking

MAMIBUSSOL

Board Regular
Joined
Jun 2, 2011
Messages
95
Hi

I am using the following code, and would like to put in some error checking.

code being used is:

Sub enterstartdate(startdate)
Do
startdate = InputBox("ENTER STARTDATE AS DD/MM/YYYY ie 01/01/2010", "STARTDATE")
If Len(startdate) = 0 Then Exit Sub
Loop Until IsDate(startdate)

startdate = CDate(startdate)
End Sub

for example

I want to force them to enter a date in the format 01/10/2010 if they enter anything else I want to repeat the request

If they click on the cancel button, I want it to exit the macro
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Perhaps:
Code:
Sub EnterStartDate()
    Dim blnValidDate As Boolean: blnValidDate = False
    Dim startdate As Variant
    
    Do While Not blnValidDate
        startdate = Application.InputBox(Prompt:="Enter start date as DD/MM/YYYY", Type:=2)
        If startdate = False Then Exit Sub
        If IsDate(startdate) Then
            blnValidDate = (Format$(startdate, "dd/mm/yyyy") = startdate)
        End If
    Loop
    
    startdate = CDate(startdate)
    'proceed here
    
End Sub
 
Upvote 0
I am running the code you helped me with within a sub procedure, it exits the procedure running your code, but it doesn't exit the full macro, it just returns to the procedure that called your code, I think I need to pass

procedure 1
Call EnterStartDate(startdate)
'further coding

'exits coding below but carries on with my coding in the previous procedure

Sub EnterStartDate(startdate)
'your coding, exits this procedure on cancel, not problem
End Sub
 
Upvote 0
its ok, just being slow today, I just passed the variable back to the first procedure and placed a line of code to exit the procedure that way, thanks anyhows
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top