MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Error Handling

Posted by Jim on January 06, 2002 6:44 PM

Hi. Is there a way to see if something will be an error before it runs so a error handle isnt needed, For example I have a userform where the user needs to input a date. I they dont there is a tpe missmatch error. But instead of sending it to an error handle is there a way to say like if iserr mydate = listbox1.value to get a true false ahead of time?


Posted by Ivan F Moala on January 06, 2002 7:22 PM


On Error resume next
>>you input routine
If Err then mydate = Listbox1.value
on Error goto 0


Posted by Tom Urtis on January 06, 2002 7:23 PM


Instead of error handling per se, you might instead want to use message boxes with nested If structures, to inform the user what they need to do, and then inform them whether or not they did it correctly, all with code that checks what they type in as being a valid date or not. Here's one way to go about this:

Sub DateEntry()
Dim myDate As String
myDate = InputBox("Enter a date here:", "Date entry request")
If myDate <> "" Then
If IsDate(myDate) Then
MsgBox "Continue the macro", 64, "Valid date has been entered"
MsgBox "You did not enter a valid date.", 48, "You goofed !!"
End If
End If
End Sub

Note, if the user hits OK or Cancel on the original input box, they simply get taken back to the worksheet. If they enter data in any form besides what Excel can recognize as a date, they get a message saying No Can Do.

Hope this helps.

Tom Urtis