+ Reply to Thread
Results 1 to 4 of 4

Resume vs GoTo statement

  1. #1
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019, Microsoft 365.
    Posts
    440

    Resume vs GoTo statement

    Hi, I found a post which said:
    • Using Goto doesn't clear the Err object (so Err.Clear is necessary if you use Goto) and it leaves your error handler disabled. If an error occurs after the Cleanup label, it won't be handled at ErrorHandler.
    • Using Resume clears the Err object and it switches your error handler back on (it is disabled while it is handling errors). If an error occurs after the Cleanup lable, it will be handled at ErroHandler

    The link to this one is below:

    HTML Code: 
    I am new to VBA and still learning. Are the above statements correct?

    Following that principle, the Err.Clear statement in the code block is not needed because the Resume statement automatically clears it. Is this right?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Resume vs GoTo statement. Error Handling in VBA ;) WALOFB ;) WTF is he talking about ;)

    billj ( that would not be Bill Jelly , my Old mate would it ?? )
    Error handling in VBA cannot easily and quickly be explained.
    You will need to carefully read up on it, for example here:
    http://excelmatters.com/2015/03/17/on-error-wtf/
    and here:
    https://app.box.com/s/8zkhjcmbxrqnlnexqpktuy41clgqm4zo

    But let me try to help a bit with emphasis the part of it you have asked about

    I think you are possibly mixing up two different things:
    _1) the Error situation, that is to say what is actually going on after an Error and / or the Error handling of that situation

    and
    _2) Err Object.
    Err Object has two Methods and a few Properties
    Err is not directly related to the actual Error
    The main purpose of Err is to hold information about the last Error which occurred.
    Err is an Object that you could think of as a Form or questionnaire that has not yet been filled in. Or think of it as a register of information about the last error that occured
    When an error occurs it is updated with various information about the Error.
    For example, the_..
    Err.Description
    _..Property
    will return you a string of text to describe the Error. So you often see this at an error handling Code section:
    MsgBox "Your error was " & Err.Description & "." 'Use Description Property of Object Err to indicate Error

    You can use the Err.Clear Method to clear the registers, that is to say remove all the information of the error held within Err Object. But note_...
    http://excelmatters.com/2015/03/17/o...comment-188401
    _.. your information is incorrect. In fact the Err Object is cleared by an error statement such as
    On Error GoTo some_label/line_number

    I expect you were referring to the Error situation. That is not cleared by such an Error Handling Statement
    The Error situation is much more difficult to explain.
    That Error situation has nothing directly to do with the Err Object.
    At the occurrence or an error, the Err Object registers are filled with string information about the Error which occurred. That is all.
    ( You do have the Err Method Err.Raise, which allows you to fool VBA into thinking an error has occurred when it has not )


    _................................
    _......_____________________


    Briefly, or as briefly as I can try to explain a lot ...

    An “Error Situation” , sometimes called an Exception, or Error exception , or an Erected Exceptional VBA state, etc..etc.. is a situation in which VBA is “thinking” something is wrong. It behaves in such a situation differently. For example it ignores any further attempts by you to define a new Error Handler. It also ignores your attempts to “switch off” or “switch back” to the default error handler.
    On Error Resume Next is a user defined Error handler which overrides the default error handler. It actually suppresses an error situation being raised, and instructs the program to continue just after the line which would have raised an “Error Situation”. In the case of using On Error Resume Next Error handler Statement, VBA has been prevented from being aroused to its Erected exceptional state of being. Because of this suppression, you will often see the Error Handler Statement On Error Resume Next being accompanied close by ** a further Error Handling Statement, On Error GoTo 0( One might use also an On Error GoTo some_label/line_number user defined error handler instead here, assigning a different user defined error handler. - Again this is possible because VBA has been prevented from being aroused to its Erected exceptional state of being. )

    On Error GoTo 0
    This Error handler Statement “goes” nowhere. It “switches off” the current user defined or “switches back” to the default error handler. It would normally only work if no Error had been encountered. But in the case of using On Error Resume Next it will work. This is because, as noted, On Error Resume Next has surprised the Arousal

    On Error GoTo some_label/line_number For example like
    On Error GoTo ErrLabelAreaCodeSection42WonkGoodEveningHowAreYouSirBollox
    On Error GoTo 200

    These Error Handling Statements do “go somewhere!”. They “goes” to the code section labelled in such a way some_label: ( One can also replace the label with a code line, like 200 in the example whether or not you have code lines in your code (You can put any number anywhere in any order in VBA) ). In this case there is no suppression of the aroused Erected Exceptional VBA state. VBA is now “thinking” something is wrong. It behaves in such a situation differently. For example it ignores any further attempts by you to define a new Error Handler. It also ignores your attempts to “switch off” or “switch back” to the default error handler. However, this error handler allows the code to continue, ( rather than stopping as is the case by the default error handler ) . It continues at the point that the code “goes to”. Typically things are done there that should be done on the case of an error.


    Resume or Resume Next
    You would typically have this at the end of your error handling code section sent to by an On Error GoTo some_label/line_number . You would use this after you have changed what may have caused the Error.
    You had better be sure you change what caused the error, as the error Erected Exceptional VBA state is cleared by these Error handling Statements and then the code goes back and tries again at the code line that errored. So you cause an infinite loop if you do not sort out at the Error Handling code section what caused your error!!!


    _--------------
    Now back to our Err Object
    Object Err
    There is an Object Err available to us. This has a couple of Methods and some Properties. These Properties are strings of information about the last type of error that was encountered. So this can be used, for example, as an indication of if an error occurred after using On Error Resume Next . Note however, If one is wanting to utilize this information in the case of a On Error Resume Next, then one should do that before an On Error GoTo some_label/line_number or a On Error GoTo 0 . **This is because, these two Error Handling Statements Clear that information. ( This clearing can also be done by a code line using the Err.Clear Method )
    Err.Clear has no effect on the current error situation. It simply clears some strings of information.



    Alan


    ' http://www.excelfox.com/forum/f23/di...-894/#post9376
    ' http://excelmatters.com/2015/03/17/o...comment-188401
    ' https://app.box.com/s/8zkhjcmbxrqnlnexqpktuy41clgqm4zo
    ' http://www.eileenslounge.com/viewtop...192781#p192781
    ' http://www.excelforum.com/excel-prog...ml#post4493107
    ' http://www.excelforum.com/excel-prog...ml#post4529827
    Last edited by Doc.AElstein; 11-26-2016 at 10:33 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  3. #3
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019, Microsoft 365.
    Posts
    440

    Re: Resume vs GoTo statement

    Hi, Doc.AElstein, really good answer with the useful link to the sources too. Thank you so much.

  4. #4
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Resume vs GoTo statement

    Hi billj,
    Thanks for the feedback and Rep thingy_ ...
    _ ... I am adding a few notes here, mostly for my own later reference to tidy up some notes of mine on Error handling in VBA.
    _...
    ( ( BTW, Initially I thought you might have been asking the difference between On Error Resume Next and On Error GoTo __Label_/-Line number
    ( although after checking here http://stackoverflow.com/questions/2...handling-block it is more clear that you were not asking that ).
    You got some good answers there.. it is a bit debateable whether one should cross post. Sometimes it can confuse things a bit. But as you gave the Link it is OK )

    _...
    _....So.. my explanations may not have quite been tailored to what you were asking . But no harm done. Often ., like me, people first hit this tricky area of VBA when a On Error GoTo Error Handle only works once..
    On Error Resume Next works. On Error GoTo only works once:
    http://www.excelforum.com/excel-new-...err-clear.html
    The answer should then be clear now that the On Error GoTo does not clear this weird “aroused exceptional error state” in which amongst other things it no longer responds any attempt to assign an Error handler. So [color=Blue]On Error GoTo ___ only works once[/U].

    _...So, anyways... I am posting a bit more for future reference as it helps me to get clear an almost infinite combination of these things error things....
    I post some codes here:
    http://www.excelforum.com/showthread...79#post4530379
    http://www.excelforum.com/showthread...=1#post4530384

    I have ‘Commented them a bit, but it is a bit much to go through until you have a good idea of the whole business. I updated my Word Doc link also ( and the codes are in it )
    https://app.box.com/s/8zkhjcmbxrqnlnexqpktuy41clgqm4zo
    ( Also I added some new notes as I learned a bit more about the Resume – I saw you using it with a following ( Label ) Argument.. I had only used it as Resume or Resume Next )
    Along these sort of lines.....
    So I see we have:
    Resume 'Will go back and try again ( For an error in a Called procedure, the procedure will be recalled from where previously Called)
    Or
    Resume Next 'Will go to the Line after that where the error occurred . ( For an error in a Called procedure, the code will continue as if the Called procedure had been ignored ( skipped over ) )
    Or
    Resume __Label_/-Line_number 'Will go where specified

    All the above 3 will either reset or probably rather leave the previous error handler enabled. I am not too sure on this. The corresponding Error Handling Statement which “Resumes without going anywhere” is On Error GoTo -1. I believe this does not change the Previous error handler, but instead leaves it as it was, On. ( An Error arousal Erection Exception prevented it from working ). The difference is academic and probably no one knows for sure.

    Err Object
    Err.Raise _______
    A last point to remind me on this. I believe VBA in its workings actually pseudo uses this after an error occurs.
    So if you yourself use it, then after this usage VBA does not know the difference. That is to say as far as VBA is occurred an exceptional error situation has been raised. We have an Erection. The minimum you need to give is a valid Error Number. The other registers are not cleared. If you had a previous error , before your use of Err.Raise , and you only give an Error Number, then the Last Description will be held still. If, before your use of Err.Raise with just an Error Number, the registers are empty ( either because the Err Object had not previously been used or you did a Err.Clear to clear the registers ), then in that case VBA will fill the registers to match the Error Number. You can give any the string registers any text.


    Alan

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] I must get rid of on error resume next statement!
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-29-2016, 07:16 AM
  2. [SOLVED] Need Help avoiding GOTO statement
    By Trachr in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-16-2015, 06:05 PM
  3. VBA Job Resume statement (quick question)
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2014, 08:05 PM
  4. On error goto statement ignored
    By evenflow in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2013, 01:34 AM
  5. if and goto statement not working.
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2008, 10:30 AM
  6. If Then - Goto Statement
    By tm1274 in forum Excel General
    Replies: 1
    Last Post: 03-02-2007, 04:16 PM
  7. Goto statement in an IF function
    By Gary L Brown in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1