# Perils of GoTo



## Von Pookie (Jul 20, 2007)




----------



## MorganO (Jul 21, 2007)

That is hilarious!

I learned Basic as my first programming language 30 years ago and Goto was the only branching option available.  I have over the years grown away from using but occasionally come upon a situation where I really want to use it, and am stymied by the years of being beat over the head that Goto's were EVIL!

This cartoon captures the essence of my internal struggles perfectly!

Owen


----------



## Lewiy (Jul 23, 2007)

Is there actually a situation where you would absolutely *have* to use a GoTo? (referring to VBA in particular).  Personally I don’t even think about them when I’m writing code, I’ve never used one in VBA and never intend to!!  My earliest programming was done as a little kid using TIBasic on a Texas Instruments graphical calculator.  It was fairly limited in functionality so I got into the habit of using GoTo for most loops but my father always reprimanded me for it so I soon changed my ways and never looked back since!  God bless parents!!


----------



## Richard Schollar (Jul 23, 2007)

Well,

On Error Goto Err_Handler

utilises a Goto and my VBA apps would be much the worse for not incorporating something like this...


----------



## Joe4 (Jul 23, 2007)

Yep, a lot of people (including myself) use them in Error Handling (and it usually just to jump to the end, do some debugging and exit).  

However, that is the only place that I use them.


----------



## Norie (Jul 23, 2007)

So that's what happened to the guy in Jurassic Park.

A velociraptor found he was using GoTo unnecessarily in his code and decided to take the appropriate action.


----------



## Joe4 (Jul 23, 2007)

Kind of reminds me of Terry Tate, Office Linebacker:
http://www.youtube.com/watch?v=MRkiouh5NEI


----------



## Lewiy (Jul 24, 2007)

> So that's what happened to the guy in Jurassic Park.
> 
> A velociraptor found he was using GoTo unnecessarily in his code and decided to take the appropriate action


  


```
Sub ParkSecurity()
On Error GoTo WaitingHelicopter
If Dinosaur.Escape = True then
	FrozenEmbryos.InBag = True
	FatDude.TravelSpeed = Fast
	If ParkJeep.InRain.Reliability < Average then
		Rain = xlVeryHeavy
	End If
End If
Select Case SmallFrillyDinosaur
	Case Squeaks
		FatDude.Affection = High
	Case Growls
		FatDude.Affection = Medium
	Case Screams
		FatDude.Affection = Low
		FatDude.Pants = Brown
		FatDude.Intelligence = xlVeryStupid
End Select
If FatDude.Pants <> White then
	FatDude.Alive = False
	MsgBox “Uh, Uh, Uh, You didn’t say the magic word!!!”
End If
End Sub
```


----------



## NumbersMax (Jul 24, 2007)

VBA humor...gotta love it!!!!  :wink:      

This whole thread is hilarious...thanks guys!


----------



## HalfAce (Jul 27, 2007)

Lewiy:
I like the 'FatDude' code.

Good job.


----------



## steve case (Jul 29, 2007)

One would think from reading the above, that "GoTo" is as evil as merging cells.  

I KNOW from personal experience that it is not.  

I use "GoTo" lots, and the folks I make my little toys for think I'm a genius.
.
.
.
.


----------



## Norie (Jul 30, 2007)

Spaghetti_code


----------



## NumbersMax (Jul 30, 2007)

I remember using Goto in Basic class in the 11th grade...1988!

It was a cool tool, but even then it could be tricky with the simplest programs.  If you program wasn't working right, it was hard to find out just where the problem was because the gotos had the code going all over the place and stuff would happen so quickly, you couldn't see from the output exactly where the problem began! 

It's nice to have things like message boxes that you can place throughout code to tell you what an error is or give you progress checks along the way.  I've used them in some macros before.

I haven't the experience to say that I've used VBA and other programming code to it's fullest, but certainly the ability is out there to accomplish just about anything you can think up.  Every week, I'm learning a new function that I've never heard of before...especially from this board (KUDOS TO THE BOARD!!)  The code that gets thrown around this board is a little deep for me, but I fully intend to start learning it and trying it out!

Certainly, the FOR, WHILE and IF loops in Norie's link provide some interesting alternatives and the code was shorter (dare I say more "elegant") than the BASIC code that it replaced.

Thanks for everything!
Max


----------



## TinaP (Jul 30, 2007)

> Certainly, the FOR, WHILE and IF loops in Norie's link provide some interesting alternatives and the code was shorter (dare I say more "elegant") than the BASIC code that it replaced.



I agree.  But I can't justify spending hours figuring out a loop to deal with a condition when a fifteen minute GOTO statement will solve the problem.  I try to keep my GOTOs in check, though.


----------



## daniels012 (Jul 30, 2007)

Is the code working?

You may need to add the line:


```
On Error GoTo JurassicParkII
'    ActiveEmbryo.Unprotect
    With ActiveEmbryo.Resize(120, 65)
        With .SpecialEmbryoCells(xlCellTypeDinosaur)
            .ClearGlasses
            .LockedVeryLargeGates = False
        End With
           
        End With
'    ActiveEmbryo.Protect
    On Error GoTo Dr.John>Hammond
    ChangeDate
End Sub
```


----------



## Von Pookie (Aug 13, 2007)




----------



## Lewiy (Aug 14, 2007)

```
If User = ManiacSerialKiller Then
    GoTo DarkHouse
    GoTo Kitchen
    GoTo Drawer
    ManiacSerialKiller.HasKnife = True
    GoTo Lounge
    With Victim
        .Scream = Loud
        .RunInStupidDirection = True
    End With
    ManiacSerialKiller.ConfusedByGoTos = True
    Code.Spaghetti = xlVerySpaghettiLike
    User.Terminate = True
End If
```


----------



## ExcelChampion (Aug 14, 2007)

How about using Goto to return error handling back to Excel as in:

On Error Resume Next
'...some code
On Error Goto 0


----------

