The GoTo command is a control statement used in VBA programming to jump execution to specific points in your code.
In this page, we will explain the basic usage of the GoTo command and provide some considerations when using it.
Basic Usage of the GoTo Command
The GoTo command is written as follows:
GoTo LabelName
' Code to be skipped
LabelName:
' Code to be executed after the jump
The GoTo command is used in conjunction with a label name to jump to a specified label.
Label names can be placed anywhere in the code, but they are typically used immediately after the section where you want to skip the execution of the command or as a destination to jump to when a specific condition is met.
Considerations for the GoTo Command
Naming and Placement of Labels
Labels can be placed anywhere in the code, but they are typically used immediately after the section where you want to skip the execution of the command or as a destination to jump to when a specific condition is met.
Label names must start with a letter and may contain letters, numbers, and underscores (they are case-insensitive). Reserved words cannot be used, and a label must be unique within the same procedure. Write the label at the start of the line (no indentation) and end it with a colon (:).
Scope of Jumps
The GoTo command can only jump to labels within the same procedure (subprocedure or function). Jumps to labels in different procedures or modules are not allowed.
Code Structure and Readability
Unconditional jumps like GoTo are discouraged in many modern programming languages.
This is because the GoTo command can make the flow of a program opaque and make the code harder to read.
Therefore, to enhance code readability and maintainability, it is desirable to minimize the use of the GoTo command and instead use more structured approaches to control the flow of the program, such as If-Then-Else statements or Select Case statements, to achieve similar results.
Usage in Error Handling
The GoTo command is often used in the context of error handling.
It allows you to jump to a specified error handler when an error occurs and perform appropriate error-specific actions.
The following example uses On Error GoTo to jump to a label when an error occurs.
Sub Sample()
On Error GoTo ErrorHandler
Dim result As Long
result = 10 / 0 ' Runtime error (division by zero)
Exit Sub ' Exit here so the handler is not run on normal completion
ErrorHandler:
MsgBox "An error occurred: " & Err.Description
End Sub
If you forget Exit Sub, the error handler also runs on normal completion, so be careful.
To learn how to implement Try Catch Finally in VBA using the GoTo command, refer to the following article:
In general, it is common to perform error handling in each function and then throw to the calling code. However, implementing this in VBA is not straightforward. If you create an error handler, it will suppress the error and if you try to raise the error again from there, it will result in an infinite loop within that function. A specific example is when you want to open a file using the Open function and still raise an error if it fails, while also propagating the error to the calling code.
Summary
The GoTo command is a useful tool in VBA for conditionally skipping specific sections of code, but its usage should be approached with caution.
To maintain code readability and maintainability, it is recommended to consider GoTo as a last resort and prioritize the use of more structured control flow alternatives.