ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error Handling (https://www.excelbanter.com/excel-programming/338854-error-handling.html)

Dave M.

Error Handling
 
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.


FSt1

Error Handling
 
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

"Dave M." wrote:

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.



All times are GMT +1. The time now is 01:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com