GoSub - Return

This is used to jump to a label or line number, execute code, and then return to the previous position in code and continue execution.

Public Sub Test() 
Dim myNumber As Integer

   myNumber = InputBox("Enter a positive number to be divided by 2.")
   If (myNumber > 0) Then
      GoSub LineLabel
   End If
   Debug.Print myNumber
   Exit Sub

LineLabel:
   myNumber = myNumber / 2

   Return
End Sub

The Return will return control back to the statement after the GoSub statement.


GoTo vs GoSub

When you use the GoTo statement there is no way of remembering where you came from.
The GoSub allows you to use the Return keyword.
Most experienced programmers will not use the GoSub statement.


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext