Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Error Handling in Macros

"Don Guillett" wrote in message
You should always post your code but try inserting
on errror resume next
early in your code


That is perhaps the worst possible solution for the user's problem. A far
better solution would be first to download MZTools (www.mztools.com) or a
similar add-in that will automatically add line numbers to code. Then use On
Error Goto <label to display the error. E.g.,

Sub AAA()
Dim WS As Worksheet
On Error GoTo ErrH:
10 Set WS = Worksheets("AAA")
20 Debug.Print "Error line skipped"
30 Exit Sub
ErrH:
40 If MsgBox("Error on line: " & Erl & vbCrLf & _
"Error: " & CStr(Err.Number) & ": " & Err.Description & vbCrLf & _
"Do you want to continue?", vbYesNo) = vbYes Then
50 Resume Next ' optional. do you really want to
60 End If
End Sub

This will tell you exactly where the error occurred and you can change the
code to prompt for a sheet name or workbook name or whatever is necessary to
remedy the problem. You have the option of continuing the code execution if
desired.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)





"Don Guillett" wrote in message
...

You should always post your code but try inserting
on errror resume next
early in your code
--
Don Guillett
SalesAid Software

"Randy" wrote in message
...
I have a macro that was written long ago by someone no longer with the
company. The macro has workbook names hard coded in it that it opens
from a
share and reads one at a time. Everything runs great until the workbook
no
longer exists or the name has been changed. I'm a macro novice and am
wondering if there is a simple error routine that can be added which
would
prompt me with the error and give me the option to continue on.
Currently,
it just errors out and I'm unable to generate critical reports until I
figure
out what's wrong.

Thanks in advance!

Randy





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Error Handling in Macros

I would try something like

For i = LBound(projectfiles) To UBound(projectfiles)
On Error Resume Next
Err.Clear
Workbooks.Open P & projectfiles(i)
If Err.Number = 0 Then
Windows(projectfiles(i)).Activate
Else
MsgBox "Error on index: " & CStr(i) & vbCrlf & _
"Err: " & CStr(Err.Number) & " " & Err.Description
End If
On Error Goto 0


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Randy" wrote in message
...
Chip,

I have not been able to modify your example to work the way I need it to.
Here is the portion of my macro that I need the error routine for:

' IMPORT PROJECT STATUS REPORTS

' Loop through "projectfiles" array to insert new project status
reports.
For i = LBound(projectfiles) To UBound(projectfiles)

' First open the project status report file.
Workbooks.Open P & projectfiles(i)
Windows(projectfiles(i)).Activate

When the macro cannot find the status report named in "projectfiles", I
need
a message box to display stating there is a problem with the file it is
trying to open (list the file name) and then do I want to skip this file
and
continue. I can send my whole macro if that would help.

Thanks again for the help.

Randy


"Chip Pearson" wrote:

"Don Guillett" wrote in message
You should always post your code but try inserting
on errror resume next
early in your code


That is perhaps the worst possible solution for the user's problem. A
far
better solution would be first to download MZTools (www.mztools.com) or
a
similar add-in that will automatically add line numbers to code. Then use
On
Error Goto <label to display the error. E.g.,

Sub AAA()
Dim WS As Worksheet
On Error GoTo ErrH:
10 Set WS = Worksheets("AAA")
20 Debug.Print "Error line skipped"
30 Exit Sub
ErrH:
40 If MsgBox("Error on line: " & Erl & vbCrLf & _
"Error: " & CStr(Err.Number) & ": " & Err.Description & vbCrLf &
_
"Do you want to continue?", vbYesNo) = vbYes Then
50 Resume Next ' optional. do you really want to
60 End If
End Sub

This will tell you exactly where the error occurred and you can change
the
code to prompt for a sheet name or workbook name or whatever is necessary
to
remedy the problem. You have the option of continuing the code execution
if
desired.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)





"Don Guillett" wrote in message
...

You should always post your code but try inserting
on errror resume next
early in your code
--
Don Guillett
SalesAid Software

"Randy" wrote in message
...
I have a macro that was written long ago by someone no longer with the
company. The macro has workbook names hard coded in it that it opens
from a
share and reads one at a time. Everything runs great until the
workbook
no
longer exists or the name has been changed. I'm a macro novice and am
wondering if there is a simple error routine that can be added which
would
prompt me with the error and give me the option to continue on.
Currently,
it just errors out and I'm unable to generate critical reports until I
figure
out what's wrong.

Thanks in advance!

Randy







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
HOW TO DISABLE ALL MACROS in BeforeClose event handling procedure Intellihome[_21_] Excel Programming 7 June 14th 05 03:38 PM
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM
Error handling Gareth Excel Programming 1 July 11th 04 07:34 PM
Error Handling bw Excel Programming 3 June 20th 04 06:43 PM


All times are GMT +1. The time now is 11:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"