PDA

View Full Version : GoTo vs Call



Herbiec09
05-14-2013, 09:41 PM
Morning All,

Just a quick question, what is the difference between between GoTo and Call when directing a code to another subroutine? Is any better than the other and are there any implications on resource utilisation as the Call seems to really slow my machine down.

Thanks

H

GTO
05-14-2013, 10:02 PM
Greetings Herbie,

Please see here: http://www.excelguru.ca/content.php?184-A-message-to-forum-cross-posters

I see that you just joined and I think that Ken Puls does a great job in explaining when cross-posting (and how) might be necessary/appropriate.

Thank you so much,

Mark

For others: Cross-posted here (http://www.mrexcel.com/forum/excel-questions/702779-goto-vs-call.html)

Jan Karel Pieterse
05-15-2013, 12:37 AM
Goto only allows you to have your code jump to a line within the same routine. Call calls another routine.
Use of Goto in VBA programming is generally considered bad practise, as it may lead to code that is quite hard to understand.
Generally speaking, try to keep your routines small: no more than e.g. one or two screens high on your monitor. If a routine gets (a lot) bigger, it is time to split it into multiple routines, each with a dedicated function. Not that I always adhere to my own advice, but there you go anyway :-)

Paul_Hossler
05-15-2013, 09:42 AM
as the Call seems to really slow my machine down.


I think that the very slight number of extra cycles required to 'Call' instead of 'Goto' would be imperceptable

If you are noticing a slowdown, I would expect that there's another issue.

The structured advantages of modularizing and using 'Call' make maintainability much easier.

Paul

SamT
05-15-2013, 09:19 PM
Error handling
On Error Goto ErrorHandler
'A bunch code here

Exit Sub ' Prevent entering Error Handler when no error occurs
ErrorHandler:
'Do stuff for error
End Sub

Possible endless loop
Do While X > Y
i = 1
'Perform Weird math on X and Y
i = i + 1
If i > 1000 Go To GetOutofLoop
Loop
GetOutOfLoop:

Loop to perform action on all but something different
For each Sht in Worksheet
If Sht = ThisWorksheet Then Goto EndOfLoop
''Do unto other sheets
EndOfLoop:
Next Sht

Jan Karel Pieterse
05-16-2013, 12:42 AM
@Sam:

I prefer to do that without GoTo, tidier if you ask me:


For Each Sht In Worksheets
If Sht.Name <> Worksheets("IgnoreThisOne").Name Then
''Do unto other sheets
End If
Next Sht
NB: There is no such object as ThisWorksheet built-in into Excel's object model, hence the change.

Aflatoon
05-16-2013, 01:28 AM
Although Goto can be useful if used judiciously it seems to be more often used to create this sort of code flow
Sub spaghetti()
Dim n As Long

For n = 1 To 5
On n GoTo label1, label2, label3, label4, label5
restart:

Next n
Exit Sub

label1:
MsgBox "n is 1"
GoTo restart

label2:
MsgBox "n is 2"
GoTo restart

label3:
MsgBox "n is 3"
GoTo restart

label4:
MsgBox "n is 4"
GoTo restart

label5:
MsgBox "n is 5"
GoTo restart

End Sub

magelan
05-16-2013, 06:46 AM
Although Goto can be useful if used judiciously it seems to be more often used to create this sort of code flow
Sub spaghetti()
Dim n As Long

For n = 1 To 5
On n GoTo label1, label2, label3, label4, label5
restart:

Next n
Exit Sub

label1:
MsgBox "n is 1"
GoTo restart

label2:
MsgBox "n is 2"
GoTo restart

label3:
MsgBox "n is 3"
GoTo restart

label4:
MsgBox "n is 4"
GoTo restart

label5:
MsgBox "n is 5"
GoTo restart

End Sub


This made my brain hurt

:eek:

SamT
05-16-2013, 08:22 AM
Jan,

Thanks for the clarification.

Yes, I should have found a better example, but that was at the end of a very long day.

ThisWorkSheet was meant as a variable. I should have used ThisWkSht to be clearer.

ps: there's no "GoTo' in your example :cloud9:

Aflatoon
05-16-2013, 08:28 AM
This made my brain hurt

:eek:

Precisely. :)

Paul_Hossler
05-16-2013, 06:37 PM
The last time I saw a computed GoTo was in the mid-70's:yes

But no rule should (IMHO) be followed, and only broken after careful consideration and for good reason (see every post above)

I only use GoTo within a loop to avoid having hard to deeply nested If/Then/Else's, and to just get the next iteration. To me the increase in readability and follow-ability is worth it



Sub GoToIsProbablyOK_Sometimes()
Dim iRow As Long

With ActiveSheet.Cells(1, 1).CurrentRegion
For iRow = 2 To .Rows.Count

If .Cells(iRow, 1).Value = "A" Then GoTo NextRow
If .Cells(iRow, 3).Value < 7 Then GoTo NextRow
If .Cells(iRow, 5).Value > 1234 Then GoTo NextRow
If Len(.Cells(iRow, 7).Value) = 0 Then GoTo NextRow
If .Cells(iRow, 9).Value = .Cells(iRow - 1, 8).Value Then GoTo NextRow

'do something here
NextRow:
Next iRow
End With
End Sub


I've followed other discussions about the use of Exit Sub's in a module

I just guess that using the permitted (=only) On Error GoTo .... statement for error handling in VBA confuses the issue

Paul

Jan Karel Pieterse
05-17-2013, 02:03 AM
ps: there's no "GoTo' in your example
I know, I thought since the OP asked about the advantages/disadvantages of using goto vs call I'd add the "proper" way to do things like that. I know you know how to :yes

SamT
05-17-2013, 06:35 AM
Jan,

It is participating in discussions like this one, about things that appear trivial, that lets me learn about the subtle nuances of VBA.

My thanks to you and all the others here at VBA Express,
SamT