VBA GoTo a Line Label

The GoTo Statement in VBA allows you to jump to a line of code.

First create a line label anywhere in your code:

Then add to “GoTo” statement to jump to the line label

GoTo Examples

This example tests the year. If the year is 2019 or later it will GoTo the Skip line label. This allows you to skip over code if certain conditions are met.

 

GoTo Multiple Line Labels

You can also use GoTo statements to jump to relevant lines of code.  Let’s adjust our previous example to go to different code locations based on which year it is:

Notice the “GoTo EndProc” before each line label. We added this line of code so that those code sections are skipped unless they are accessed by the relevant “GoTo”.

GoTo Error Handler End of Procedure

Now let’s use Error Handling to go to the end of the procedure if there is an error.

 

GoTo Repeat Code

Our last example will use the GoTo Statement to repeat some code.

Below we’re using a Yes / No Messagebox (Click to learn more) to confirm that the user acknowledges the warning. If they click ‘No’, the message box will be displayed again until they click ‘Yes’ (see GIF below).

go to line label

VBA GoTo a Line Label in Access VBA

All of the above examples work exactly the same in Access VBA as in Excel VBA.

 

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)