locked
If....Then.....Else and the GoTo statement RRS feed

  • Question

  • I have a nested if statement set up like the following:

    Some Code
    
    If.....Then
    
        If......Then GoTo SomePlace
    
        ElseIf....
    
            Do This
    
        ElseIf.....
    
            Do That
    
        EndIf
    
    ElseIf......<-----
    
        Some Code
    
    EndIf
    
    Some More Code
    
    SomePlace:
    
    Even More Code

    The problem I am facing is whenever the GoTo statement is added as shown above in the nested if statement I get an error message telling me that there is an Else without an If in my code (pointing to the Else that is associated with the outside if statement (as shown by the arrow above)....any ideas why this would happen? Also, is there a better way to skip code in the manner in which I am showing above rather than using the GoTo statement?

    Thanks



    • Edited by ChemEng Thursday, July 4, 2013 3:02 PM Clarity
    Thursday, July 4, 2013 2:51 PM

Answers


  • Move "GoTo Someplace" down one line...

     If......Then 
    GoTo SomePlace ElseIf....

    Also, "it depends", but you could wrap the entire code set in an If - End If...
      If Not DogsAreGreen then
        'Lots of code
      End If

    '---
    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel programs)


    • Marked as answer by ChemEng Thursday, July 4, 2013 3:37 PM
    • Edited by James Cone Sunday, October 30, 2016 8:13 PM
    Thursday, July 4, 2013 3:24 PM