VBA GoTo a Line Label

Associated Files Download Links

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

 

 

 

You may also like some of this related content...

Advertisements
Automate Excel
Left Menu Icon