VBA "On Error" Statement Not Being Recognised

Chris Mack

Well-known Member
Joined
Jun 18, 2013
Messages
803
Hi all

I have the following code:

Code:
Private Sub Workbook_Open()
    Dim InitialMonth As String, Restarted As Boolean
    Restarted = False
    InitialMonth = Format(Date - 1, "mmmm yyyy")
    GoTo Start
Restart:
    Restarted = True
Start:
    If Restarted = True Then
        AlternateMonth = Application.InputBox("""" & AlternateMonth & """ is not a valid entry." & vbNewLine & vbNewLine & _
            "I am about to process " & InitialMonth & "." & vbNewLine & vbNewLine & _
            "To process a different month, please enter it here in the format ""mmm-yy"" (must be later than Apr-15) (otherwise, click OK to continue):")
    Else
        AlternateMonth = Application.InputBox("I am about to process " & InitialMonth & "." & vbNewLine & vbNewLine & _
            "To process a different month, please enter it here in the format ""mmm-yy"" (must be later than Apr-15) (otherwise, click OK to continue):")
    End If
    On Error GoTo Restart
    If AlternateMonth = False Then
        MsgBox "Cancelled"
'        ActiveWorkbook.Save
'        Application.Quit
    ElseIf AlternateMonth = "" Then
        FirstOfMonthToProcess = DateValue("1 " & InitialMonth)
    ElseIf DateValue("1-" & AlternateMonth) < DateValue("1-Apr-15") Then
        GoTo Restart:
    Else
        FirstOfMonthToProcess = DateValue("1-" & AlternateMonth)
    End If
'    Sheets("Trust").[A15] = MonthToProcess
End Sub
If I type in a random text string into the input box, the error handler works fine the first time around ("On Error GoTo Restart"). However, if I type the same string again, I get a run time error when VBA attempts to evaluate the line "ElseIf DateValue("1-" & AlternateMonth) < DateValue("1-Apr-15") Then".

It definitely passes the error handling line after each iteration of an InputBox, so what is the problem here?

Many thanks

Chris
 
I was also thinking of using IsDate instead of error handles, took me a while to get things in the right order though.

This also removed the need to step backwards in the code.

Code:
Private Sub Workbook_Open()
    Dim InitialMonth As String
    InitialMonth = Format(Date - 1, "mmmm yyyy")
    
    AlternateMonth = Application.InputBox("I am about to process " & InitialMonth & "." & vbNewLine & vbNewLine & _
        "To process a different month, please enter it here in the format ""mmm-yy"" (must be later than Apr-15) (otherwise, click OK to continue):")
    
    If AlternateMonth = False Then
        MsgBox "Cancelled"
'        ActiveWorkbook.Save
'        Application.Quit
    ElseIf AlternateMonth = "" Then
        FirstOfMonthToProcess = DateValue("1 " & InitialMonth)
    Else
        Do
            If IsDate(AlternateMonth) Then
                If DateValue("1-" & AlternateMonth) >= DateValue("1-Apr-15") Then
                    FirstOfMonthToProcess = DateValue("1-" & AlternateMonth)
                    Exit Do
                End If
            End If
            AlternateMonth = Application.InputBox("""" & AlternateMonth & """ is not a valid entry." & vbNewLine & vbNewLine & _
                "I am about to process " & InitialMonth & "." & vbNewLine & vbNewLine & _
                "To process a different month, please enter it here in the format ""mmm-yy"" (must be later than Apr-15) (otherwise, click OK to continue):")
        Loop
    End If
'    Sheets("Trust").[A15] = MonthToProcess
End Sub
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

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