Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default error handling

Could somebody please explain what the following fragment of code does with
a function i've seen.


.............................
On error resume next

cellreference.comment.delete

on error goto 0

..................


goto ??????


I think i'm correct in thinking the "on error resume next" statement means
any errors after this statement and before the end of the function should be
ignored.
for example caused by the delete of a non existent comment.

But whats the goto all about?

Thanks
James


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default error handling

Hi James

Goto is the ability to jump to specific named ranges of your code, usually
done like

Sub test1()
On Error GoTo MyError
MsgBox 45 / 0
Exit Sub
MyError:
MsgBox "snafu"
End Sub

Extensive use of Goto makes so called "spaghetti code", amateur code totally
impossible to read and manintain. Like

Sub test2()
Dim i As Long
i = Val(InputBox("Number:"))
If i = 0 Then GoTo C
A:
MsgBox "You said " & i
If i 10 Then GoTo D
B:
MsgBox "i is now " & i
Exit Sub
C:
MsgBox "Hi C"
GoTo A
D:
i = i / 5
GoTo B
End Sub

The only reasonable use of goto is what I showed in test1; if something
happens then jump to the end and do the cleanup there. Avoid spaghetti.

Goto 0 cancels all previous error handling and lets the application take
care of it instead. As in

Sub test3()
On Error GoTo MyError
MsgBox 45 / 1
On Error GoTo 0
MsgBox 45 / 0
Exit Sub
MyError:
MsgBox "snafu"
End Sub

HTH. Best wishes Harald



"James Cornthwaite" skrev i melding
...
Could somebody please explain what the following fragment of code does

with
a function i've seen.


............................
On error resume next

cellreference.comment.delete

on error goto 0

.................


goto ??????


I think i'm correct in thinking the "on error resume next" statement means
any errors after this statement and before the end of the function should

be
ignored.
for example caused by the delete of a non existent comment.

But whats the goto all about?

Thanks
James




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default error handling

ah got it!

thats smashing
thanks

"Harald Staff" wrote in message
...
Hi James

Goto is the ability to jump to specific named ranges of your code, usually


done like

Sub test1()
On Error GoTo MyError
MsgBox 45 / 0
Exit Sub
MyError:
MsgBox "snafu"
End Sub

Extensive use of Goto makes so called "spaghetti code", amateur code
totally
impossible to read and manintain. Like

Sub test2()
Dim i As Long
i = Val(InputBox("Number:"))
If i = 0 Then GoTo C
A:
MsgBox "You said " & i
If i 10 Then GoTo D
B:
MsgBox "i is now " & i
Exit Sub
C:
MsgBox "Hi C"
GoTo A
D:
i = i / 5
GoTo B
End Sub

The only reasonable use of goto is what I showed in test1; if something
happens then jump to the end and do the cleanup there. Avoid spaghetti.

Goto 0 cancels all previous error handling and lets the application take
care of it instead. As in

Sub test3()
On Error GoTo MyError
MsgBox 45 / 1
On Error GoTo 0
MsgBox 45 / 0
Exit Sub
MyError:
MsgBox "snafu"
End Sub

HTH. Best wishes Harald



"James Cornthwaite" skrev i melding
...
Could somebody please explain what the following fragment of code does

with
a function i've seen.


............................
On error resume next

cellreference.comment.delete

on error goto 0

.................


goto ??????


I think i'm correct in thinking the "on error resume next" statement
means
any errors after this statement and before the end of the function should

be
ignored.
for example caused by the delete of a non existent comment.

But whats the goto all about?

Thanks
James






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
Error Handling Francis Brown Excel Programming 0 November 30th 05 06:17 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 V. Roe Excel Programming 2 February 27th 04 08:04 PM


All times are GMT +1. The time now is 06:00 PM.

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"