Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default OnError - what generated the error?

A simple problem. If an OnError routine is invoked, how can you tell which
instruction caused the error?

There must be an easy way, but I can't find it. Single-stepping is not what
I mean, I just want there to be a field somewhere that tells me the previous
instruction executed.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200808/1

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default OnError - what generated the error?

You can use line numbers (as in old-style Basic) and the
Erl (Error Line) variable for this:

Sub Errorhandling()
On Error GoTo ErrorHandler
10 Dim x As Integer
20 x = 0
30 x = 1 / x
Exit Sub
ErrorHandler:
MsgBox "Error occurred on line " & Erl
End Sub


There are some add-ins available to add line numbers
automatically - Google should find them.



On 10 Aug, 08:39, "Robeyx via OfficeKB.com" <u43777@uwe wrote:
A simple problem. *If an OnError routine is invoked, how can you tell which
instruction caused the error?

There must be an easy way, but I can't find it. *Single-stepping is not what
I mean, I just want there to be a field somewhere that tells me the previous
instruction executed.

--
Message posted via OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/excel-programming/200808/1


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default OnError - what generated the error?

You have 3 pieces of information that you can use. The first two are
straight-forward, Err.Number and Err.Description. The third needs to have
line numbers added to utilise

Sub TestError()
Dim Somevar As Double

10 On Error GoTo TestError_Error

20 Somevar = 7
30 Somevar = Somevar + 12
40 Somevar = Somevar / 0

TestError_Exit:
50 Exit Sub

TestError_Error:
60 MsgBox "Error: " & vbNewLine & _
" number: " & Err.Number & vbNewLine & _
" description: " & Err.Description & vbNewLine & _
" line number: " & Erl
70 Resume TestError_Exit

End Sub

Personally, I have never found Erl necessary, the error description usually
is enough. You could also add a procedure name constant and display that
value as well.

If you get MZTools, a free VBE utility from
http://www.mztools.com/v3/download.aspx , this has a function to
automatically add and remove line numbers.

--
__________________________________
HTH

Bob

"Robeyx via OfficeKB.com" <u43777@uwe wrote in message
news:887367d1bc4fe@uwe...
A simple problem. If an OnError routine is invoked, how can you tell which
instruction caused the error?

There must be an easy way, but I can't find it. Single-stepping is not
what
I mean, I just want there to be a field somewhere that tells me the
previous
instruction executed.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200808/1



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default OnError - what generated the error?

I generally try to build my code so that I use "On Error" only when
absolutely necessary, otherwise, it can take some time to find the problem.

Sub Macro1()

Dim myWS As Worksheet

On Error Resume Next
Set myWS = Worksheets("Sheet4")
On Error GoTo 0

If myWS Is Nothing Then
MsgBox ("Worksheet doesn't exist")
Exit Sub
End If

'Continue execution if the worksheet exists

End Sub

In your current situation, can you temporarily turn off the Resume next line
to see where it errors out?

--
HTH,
Barb Reinhardt



"Robeyx via OfficeKB.com" wrote:

A simple problem. If an OnError routine is invoked, how can you tell which
instruction caused the error?

There must be an easy way, but I can't find it. Single-stepping is not what
I mean, I just want there to be a field somewhere that tells me the previous
instruction executed.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200808/1


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default OnError - what generated the error?

If you use line numbers in the code (and MZTools -- www.mztools.com -- can
do this with one mouse click), you can use the Erl function to get the line
number on which the error occured. For example,

'[Code Without Line Numbers]
Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long
On Error GoTo EndProc:
X = 1
Y = 0
Z = 0
Z = X / Y '--- cause an error
Exit Sub
EndProc:
MsgBox "Error On Line: " & Erl '--- displays "Error On Line 0"
End Sub

'[Code With Line Numbers]
Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long
On Error GoTo EndProc:
10 X = 1
20 Y = 0
30 Z = 0
40 Z = X / Y ' -- cause an error
50 Exit Sub
EndProc:
60 MsgBox "Error On Line: " & Erl '--- displays "Error On Line 40"
End Sub

MZTools, which does far far far more things than just line numbers, is a
"must have" for serious VBA development. Versions are available for VBA,
VB6, and VBNET.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)











"Barb Reinhardt" wrote in message
...
I generally try to build my code so that I use "On Error" only when
absolutely necessary, otherwise, it can take some time to find the
problem.

Sub Macro1()

Dim myWS As Worksheet

On Error Resume Next
Set myWS = Worksheets("Sheet4")
On Error GoTo 0

If myWS Is Nothing Then
MsgBox ("Worksheet doesn't exist")
Exit Sub
End If

'Continue execution if the worksheet exists

End Sub

In your current situation, can you temporarily turn off the Resume next
line
to see where it errors out?

--
HTH,
Barb Reinhardt



"Robeyx via OfficeKB.com" wrote:

A simple problem. If an OnError routine is invoked, how can you tell
which
instruction caused the error?

There must be an easy way, but I can't find it. Single-stepping is not
what
I mean, I just want there to be a field somewhere that tells me the
previous
instruction executed.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/excel-programming/200808/1





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default OnError - what generated the error?

I should have added that it is not necessary to assign each line a line
number. When an error occurs, Erl will return the most recent line number
encountered before the error arises. For example,

Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long

On Error GoTo ExitSub:
1 ' Init Block
X = 1
Y = 0
Z = 3
2 ' Math Block
X = X + 1
Z = Z + 1
3 ' Calc Block
X = X + 1
Z = X / Y
Exit Sub
ExitSub:
Debug.Print "Error (" & Err.Number & " - " & _
Err.Description & " at block: " & Erl
End Sub

Here, the most recent line number prior to the error condition is displayed
as
Error (11 - Division by zero at block: 3

This is useful if you need just to determine which sectcion of the code
caused the error, not necessarily the exact line number.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)





"Chip Pearson" wrote in message
...
If you use line numbers in the code (and MZTools -- www.mztools.com -- can
do this with one mouse click), you can use the Erl function to get the
line number on which the error occured. For example,

'[Code Without Line Numbers]
Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long
On Error GoTo EndProc:
X = 1
Y = 0
Z = 0
Z = X / Y '--- cause an error
Exit Sub
EndProc:
MsgBox "Error On Line: " & Erl '--- displays "Error On Line 0"
End Sub

'[Code With Line Numbers]
Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long
On Error GoTo EndProc:
10 X = 1
20 Y = 0
30 Z = 0
40 Z = X / Y ' -- cause an error
50 Exit Sub
EndProc:
60 MsgBox "Error On Line: " & Erl '--- displays "Error On Line 40"
End Sub

MZTools, which does far far far more things than just line numbers, is a
"must have" for serious VBA development. Versions are available for VBA,
VB6, and VBNET.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)











"Barb Reinhardt" wrote in
message ...
I generally try to build my code so that I use "On Error" only when
absolutely necessary, otherwise, it can take some time to find the
problem.

Sub Macro1()

Dim myWS As Worksheet

On Error Resume Next
Set myWS = Worksheets("Sheet4")
On Error GoTo 0

If myWS Is Nothing Then
MsgBox ("Worksheet doesn't exist")
Exit Sub
End If

'Continue execution if the worksheet exists

End Sub

In your current situation, can you temporarily turn off the Resume next
line
to see where it errors out?

--
HTH,
Barb Reinhardt



"Robeyx via OfficeKB.com" wrote:

A simple problem. If an OnError routine is invoked, how can you tell
which
instruction caused the error?

There must be an easy way, but I can't find it. Single-stepping is not
what
I mean, I just want there to be a field somewhere that tells me the
previous
instruction executed.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/excel-programming/200808/1




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default OnError - what generated the error?

I'll have to remember this. Thanks

Barb Reinhardt



"Chip Pearson" wrote:

I should have added that it is not necessary to assign each line a line
number. When an error occurs, Erl will return the most recent line number
encountered before the error arises. For example,

Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long

On Error GoTo ExitSub:
1 ' Init Block
X = 1
Y = 0
Z = 3
2 ' Math Block
X = X + 1
Z = Z + 1
3 ' Calc Block
X = X + 1
Z = X / Y
Exit Sub
ExitSub:
Debug.Print "Error (" & Err.Number & " - " & _
Err.Description & " at block: " & Erl
End Sub

Here, the most recent line number prior to the error condition is displayed
as
Error (11 - Division by zero at block: 3

This is useful if you need just to determine which sectcion of the code
caused the error, not necessarily the exact line number.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)





"Chip Pearson" wrote in message
...
If you use line numbers in the code (and MZTools -- www.mztools.com -- can
do this with one mouse click), you can use the Erl function to get the
line number on which the error occured. For example,

'[Code Without Line Numbers]
Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long
On Error GoTo EndProc:
X = 1
Y = 0
Z = 0
Z = X / Y '--- cause an error
Exit Sub
EndProc:
MsgBox "Error On Line: " & Erl '--- displays "Error On Line 0"
End Sub

'[Code With Line Numbers]
Sub AAA()
Dim X As Long
Dim Y As Long
Dim Z As Long
On Error GoTo EndProc:
10 X = 1
20 Y = 0
30 Z = 0
40 Z = X / Y ' -- cause an error
50 Exit Sub
EndProc:
60 MsgBox "Error On Line: " & Erl '--- displays "Error On Line 40"
End Sub

MZTools, which does far far far more things than just line numbers, is a
"must have" for serious VBA development. Versions are available for VBA,
VB6, and VBNET.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)











"Barb Reinhardt" wrote in
message ...
I generally try to build my code so that I use "On Error" only when
absolutely necessary, otherwise, it can take some time to find the
problem.

Sub Macro1()

Dim myWS As Worksheet

On Error Resume Next
Set myWS = Worksheets("Sheet4")
On Error GoTo 0

If myWS Is Nothing Then
MsgBox ("Worksheet doesn't exist")
Exit Sub
End If

'Continue execution if the worksheet exists

End Sub

In your current situation, can you temporarily turn off the Resume next
line
to see where it errors out?

--
HTH,
Barb Reinhardt



"Robeyx via OfficeKB.com" wrote:

A simple problem. If an OnError routine is invoked, how can you tell
which
instruction caused the error?

There must be an easy way, but I can't find it. Single-stepping is not
what
I mean, I just want there to be a field somewhere that tells me the
previous
instruction executed.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/excel-programming/200808/1




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
How to trap an error with onError in a for i loop Kevin Excel Programming 2 April 12th 07 10:36 PM
XML ERROR in PivotTable on generated XML spreadsheet [email protected] Excel Programming 0 November 3rd 06 08:58 PM
How to remove red strikethrough generated as an error? FiscallyCOC Excel Discussion (Misc queries) 1 October 5th 05 06:56 PM
Error Generated from Array formula ExcelMonkey[_190_] Excel Programming 6 March 14th 05 12:41 PM
Finding Procedure which generated error LS[_2_] Excel Programming 3 April 12th 04 06:58 AM


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

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

About Us

"It's about Microsoft Excel"