Irratic error handling, what is the problem?

328mike

New Member
Joined
Jul 5, 2011
Messages
47
Hey all,

I am having my error handler work only sometimes and I can't figure out why. What is causing the inconsistency?

Dim DataMonth
Dim DataMonthFormatted
Dim ETCMonth

DataMonth = InputBox("Please enter a data month. (Mm/01/Yyyy)")

'Processes date information again if it is entered incorrectly
Static ErrCount As Integer
On Error GoTo ErrHandler

...more code...

Exit Sub
ErrHandler:
ErrCount = ErrCount + 1
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")
If ErrCount = 4 Then
MsgBox ("I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours Dave. ")
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")
ElseIf ErrCount = 9 Then
MsgBox ("Just what do you think you're doing, Dave? ")
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")
Else
Resume
End If
Resume

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"
"Doesn't work" in what way?

An error handler can't handle its own errors; the code will break if there are errors.

EDIT: You've declared ErrCount as static, so it retains its value between executions. Is that what you mean?
 
Last edited:
Upvote 0
The if statement in the error handler isn't working. I can get the prompt to appear indefinitely with a type mismatch error but it won't display the custom messages.
 
Upvote 0
Works fine for me after the VBE is reset to zero the ErrCount variable. Try changing Static to Dim.
 
Upvote 0
Added some complexity and am getting a new problem now...

When I run my code I get error 20 when I enter a correct answer. I am not sure why it's not working but I assume it is because I have multiple exit sub commands. Any advice here?

Exit Sub
ErrHandler:
Dim AnswerErr As Integer
ErrCount = ErrCount + 1
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")

'Easter Egg
If ErrCount = 4 Then
MsgBox "I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours Dave. ", vbCritical, "HAL"
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")
If DataMonth = False Then
AnswerErr = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
If AnswerErr = 6 Then
DataMonth = ""
Exit Sub
Else
GoTo ErrHandler
End If
Else
GoTo ErrHandler
End If
ElseIf ErrCount = 9 Then
MsgBox "Just what do you think you're doing, Dave? ", vbCritical, "HAL"
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")
If DataMonth = False Then
Answer = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
If Answer = 6 Then
DataMonth = ""
Exit Sub
Else
GoTo ErrHandler
End If
Else
On Error GoTo ErrHandler
End If
ElseIf ErrCount = 14 Then
MsgBox "Cake, and grief counseling, will be available at the conclusion of the test. ", vbExclamation, "GLaDOS"
DataMonth = Application.InputBox _
("Please enter the closest date that resembles " & _
DataMonth & _
", in format (Mm/01/Yyyy)", "Invalid Date Format!")
If DataMonth = False Then
Answer = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
If Answer = 6 Then
DataMonth = ""
Exit Sub
Else
GoTo ErrHandler
End If
Else
On Error GoTo ErrHandler
End If
ElseIf DataMonth = False Then
Answer = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
If Answer = 6 Then
DataMonth = ""
Exit Sub
Else
GoTo ErrHandler
End If
End If
Resume
End Sub
 
Upvote 0
Have you tried setting a breakpoint and stepping through the code?
 
Upvote 0
I haven't.

I'm thinking that when my code hits a confirmation box to terminate the error handling I am not letting the 'no' option run to the end of the error handler, thus making that Resume not included in the process and triggering the error code. I will filter out all of the GoTo commands and try it again.
 
Upvote 0
Ok I've figured out the problem. Apparently when you click 'No' on a yes / no box and send from no to error handler it won't work because it doesn't recognize an error, I made a fix for it but I wish their was another way to do it because what I have done seems inefficient and incorrect.

Code:
'Begin Error Handling
Exit Sub
ErrHandler:
ErrCount = ErrCount + 1
DataMonth = Application.InputBox _
        ("Please enter the closest date that resembles " & _
        DataMonth & _
        ", in format (Mm/01/Yyyy)", "Invalid Date Format!")

'Easter Egg
If ErrCount = 4 Then
MsgBox "I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours Dave. ", vbCritical, "HAL"
DataMonth = Application.InputBox _
        ("Please enter the closest date that resembles " & _
        DataMonth & _
        ", in format (Mm/01/Yyyy)", "Invalid Date Format!")
        If DataMonth = False Then
            AnswerErr = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
                If Answer = 6 Then
                    DataMonth = ""
                    GoTo Terminate
                Else
                DataMonth = "ThisWillTriggerErrHandler"
                End If
        End If
ElseIf ErrCount = 9 Then
MsgBox "Just what do you think you're doing, Dave? ", vbCritical, "HAL"
DataMonth = Application.InputBox _
        ("Please enter the closest date that resembles " & _
        DataMonth & _
        ", in format (Mm/01/Yyyy)", "Invalid Date Format!")
        If DataMonth = False Then
            Answer = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
                If Answer = 6 Then
                    DataMonth = ""
                    GoTo Terminate
                Else
                DataMonth = "ThisWillTriggerErrHandler"
                End If
        End If
ElseIf ErrCount = 14 Then
MsgBox "Cake, and grief counseling, will be available at the conclusion of the test. ", vbExclamation, "GLaDOS"
DataMonth = Application.InputBox _
        ("Please enter the closest date that resembles " & _
        DataMonth & _
        ", in format (Mm/01/Yyyy)", "Invalid Date Format!")
        If DataMonth = False Then
            Answer = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
                If Answer = 6 Then
                    DataMonth = ""
                    GoTo Terminate
                 Else
                 DataMonth = "ThisWillTriggerErrHandler"
                End If
        End If
ElseIf DataMonth = False Then
Answer = MsgBox("Are you sure you don't want to enter a data month? LinkUp Eligibility will not be calculated!", vbYesNo, "Cancel Clicked")
    If Answer = 6 Then
        DataMonth = ""
        GoTo Terminate
        Else
        DataMonth = "ThisWillTriggerErrHandler"
    End If
End If
Resume
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

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