Skip Code Lines & Go To Another

  • just wondering how i can get my GoTo function working in this VBA macro.


    Code
    'Get the relative column number where the criteria should be found
        If vCriteria = vbYes Then GoTo 39


    should that not shoot me back to line 39? i get a error "label not defined" as it is. also tried to set a :AddNew flag at line 39 and point the GoTo to AddNew. the same thing happens. any help? this is bugging me as i'm used to this command working... doesnt like it in vba. perhaps i'm rusty?


    oh and one last thing... this code is at the end of the program. i'm trying to point it back tot he top to enable the user to add more email recipients. how can that redirect happen if my goto isnt working for me?

  • Re: Goto Command?


    You need to provide a name label for the GoTo. (But note that this can lead to bad programming structure. Make sure you aren't creating an infinite loop.)

    Code
    Sub test()
    Dim i As Integer
    i = 1
    MyLine:
    MsgBox "There"
    i = i + 1
    If i < 3 Then GoTo MyLine
    End Sub
  • Re: Goto Command?


    Hi,


    If you haven't done so, you need to define the line number you want to refer to in the beginning of the line like this:


    Code
    If vCriteria = vbYes Then Goto 39
    
    
    '...code...
    
    
    39 'Jump here if vCriteria returns vbYes


    Or for Line label:


    Code
    If vCriteria = vbYes Then Goto  myLabel
    
    
    '...code...
    
    
    myLabel:
    'Jump here if vCriteria returns vbYes


    Personally, though, I don't like using GoTo too much, as it makes following the code and controlling your variables really hard in the end of the day. If you can give a little more specific description of what you are trying to do, perhaps I can help.


    Rgds,
    Jani

  • Re: Goto Command?


    Quote from thomach

    You need to provide a name label for the GoTo. (But note that this can lead to bad programming structure. Make sure you aren't creating an infinite loop.)

    Code
    Sub test()
    Dim i As Integer
    i = 1
    MyLine:
    MsgBox "There"
    i = i + 1
    If i < 3 Then GoTo MyLine
    End Sub



    tried it. processed it w/o error BUT did not send me back... just pushed on and finished the code. =( why is it not looping me back i wonder? any ideas? here is the full code:




    if you see my code... basically what i'm trying to do is direct it back for an option to add more entries to the TO: field of my email. as the code runs now, it just shoots me right past the GOTO and pushes me on to the email window. any fixes?


    thanks again

  • Re: Goto Command?




    thanks jani. i think the problem i'm having here is that i'm not trying to GOTO a lower line, but a HIGHER line. not sure if that is typically supposed to work.... perhaps that is why i'm getting label does not exist errors and line errors. surely there is a way to regress my code processing from line 194 to line 39 after a boolean is set to true via a msgbox.

  • Re: Goto Command?


    I think I figured out your problem. You have declared vCriteria6 as Boolean, although the answer is a actually numeric, vbYes and vbNo are constants that correspond to an integer (or long?). Try declaring the variable as Long instead of Boolean, should work.


    Hope this helps,
    Jani

  • Re: Goto Command?


    Quote from Fencliff

    I think I figured out your problem. You have declared vCriteria6 as Boolean, although the answer is a actually numeric, vbYes and vbNo are constants that correspond to an integer (or long?). Try declaring the variable as Long instead of Boolean, should work.


    Hope this helps,
    Jani


    aha that did it! thanks! funny i didnt think that vbYesNo wouldnt be a boolean. I assumed it would be.


    much appreciated! you are a pro ^_~

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!