Error Handling Question in VBA

cob98tp

Board Regular
Joined
Nov 18, 2004
Messages
146
Hi All,

I am writing a little menu-type operation that asks the user for a date. I have got code in place to test that what is entered in the Input box is actually a date and it all works ok. The problem is the second time the user enters a non-date, I can't seem to work out how to reset the error status - despite the Err.Clear command.

Can anybody tell me what I'm doing wrong?

Thanks again guys!

Code:
Public Sub RMOption3()

SelectDate:
    Dim a As Date
    On Error GoTo NotADate
    a = InputBox("What Date Do You Wish To Export The Data To? NOTE: Please Enter the Date in the Format dd/mm/yy", "Enter Date for Export.")
    
    If a > Date Then
        E1 = MsgBox("Date Cannot be in the Future. Please Try Again.", vbOKOnly, "Incorrect Date")
        Exit Sub
    End If
    
    If a >= Date - 62 Then
        E2 = MsgBox("Date Cannot be More Than 2 Months in the Past. Please Try Again.", vbOKOnly, "Incorrect Date")
        Exit Sub
    End If

    Dato = a
    
    Call RunMacro
    
    Exit Sub

NotADate:
    E3 = MsgBox("That is not a valid Date format, would you like to try again?", vbYesNo + 32, "Invalid Date")
    
    If E3 = vbYes Then
        Err.Clear
        GoTo SelectDate
    End If
    
    If E3 = vbNo Then
        E4 = MsgBox("Operation aborted.", 0 + 48, "Abort")
    End If

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hopefully this will help. It uses IsDate() function to check for a valid input in any format.
Code:
'==============================================================
'- MACRO TO GET DATE FROM USER IN ANY VALID FORMAT (Inputbox)
'- Keep showing inputbox until proper date entry or cancel
'- uses VBA IsDate() function to check
'- NB. If in a UserForm the 'Calendar Control' is better.
'- Brian Baulsom
'==============================================================
Sub test()
    Dim DateString As String
    Dim DateProper As Date
    Dim EntryOK As Boolean
    '----------------------------------------------------------
    '- keep showing inputbox until proper date or cancel
    EntryOK = False
    Do While EntryOK = False
        DateString = InputBox("Please enter date", "DATE ENTRY")
        '- Cancel button -> exit
        If DateString = "" Then Exit Sub
        '- check date
        If IsDate(DateString) Then
            EntryOK = True
        Else
            '- error message
            MsgBox ("That is not a valid date format." & vbCr _
                & "Please try again or Cancel.")
        End If
    Loop
    '------------------------------------------------------------
    ' change string to date
    DateProper = DateString
    '- format date as required
    DateString = Format(DateProper, "dd/mm/yyyy")
    MsgBox ("Date is " & DateString)
End Sub
'-------------------------------------------
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,026
Members
449,061
Latest member
TheRealJoaquin

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