Error Handling

D

Dave M.

I am in need of some help with Error Handling. I am new to using VBA and
have found this site to be very helpful, but just cannot find enough
information on error handling to make this make sense to me. My situation is
as follows:

I have a Scheduled task that opens a workbook containing the following code:

AutoOpen Macro
Sub Auto_Open()
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

SendKeys ("{TAB}")
SendKeys ("~")

On Error GoTo errhandler

Workbooks.Open ("C:\Documents and Settings\dminor\Desktop\query\Dispatch
Query.dqy")

errhandler:
If Err.Number = 1004 Then
Application.DisplayAlerts = False
Application.EnableEvents = False
ActiveWorkbook.Close SaveChanges:=False
Application.Quit

End If

Resume Next
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime


Application.Run ("Personal.XLS!PriorityDispatch")
Application.DisplayAlerts = True

End Sub

I have two issues with this code. The first is it only addresses the most
common error, (error 1004), but every now and then I do see another type of
error. The other issue is when I do experience the 1004 error Excel will
close everything except my personal workbook.

My questions are how I address all types of errors, and how I can close all
of the workbooks. Any help would be appreciated.
 
F

FSt1

hi
basicly you need an error handler for each error you want to handle.
identifing the error is the key. then you would do something like this.....

errhandler:
If Err.Number = 1004 Then
do somthing
else
if err.number = 5 then
do something else
else
if err.number = 3074 then
do something else
end if
end if
end if

regards
FSt1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top