×

Excel VBA GoTo Statement

GoTo Statement

he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends your program wherever you want. The statement is useful in controlling program flow, and it’s easy to create.

GoTo statement cannot be used to branch from outside a For...Next, For Each...Next, With...End With, SyncLock...End SyncLock, Try...Catch...Finally, or Using...End Using construction to a label inside.

GoTo statements are used when you wish to force the user to select any specific option. It functions as a loop and repeats the same stamen again and again unless the user uses another option.

Syntax

GoTo line

Parameter

Line (required) – This parameter represents any line label.

Example 1

Sub Goto_Exercise()
 Jmp:
 MB = MsgBox("Do you like Excel?", vbYesNo)
     If MB = vbYes Then
         MsgBox "Wow! I also like working on Excel!"
     Else
         MsgBox "Ohh! Try once and you will surely like it."
     GoTo Jmp:
     End If
 End Sub 

Output

Press F5 to run the output. As soon as, VBA code run, a msgbox will pop up, asking a question, “Do you like VBA?” with buttons, Yes or No option (as shown below).

GoTo Statement vba

We will choose the “No” option. Another msgbox pops up.

Another msgbox pops up

As soon as you will click on OK. The program jumps again to the first Msgbox, stating, “Do you like VBA?”. This will continue if you will click on No option. If you select the Yes option, another Msgbox will pop up (as shown below).

Msgbox, stating

As soon as you click OK, the program terminates.

While entering any password, if you type the wrong password, you notice the Input box appears over again till the time you enter the correct one. This can be done with VBA GOTO as well.

Example 2

Sub Goto_Exercise2()
 IB_Name = InputBox("Enter your Name: ")
 Jmp:
 IB_Pass = InputBox("Enter your password")
     'if the password does not match with Bill Gates
     'it will goto Wrong Pass section
     If IB_Pass <> "Bill Gates" Then GoTo WrongPass
         MsgBox "Welcome " + IB_Name + "You are eligible to run the program"
     Exit Sub    
 WrongPass:
     MsgBox "Sorry! Try again, Username and password do not match!"
     GoTo Jmp
 End Sub 

Output

As you will run the code. An InputBox will pop out, asking for your name. Type your name and click OK.

Once you submit the name, another box appears asking for your password.

you submit the name

If your password matches with “Bill Gates” a MsgBox will appear stating your eligibility. Else for the wrong password, it will intimidate you and again ask for the right password.

MsgBox will appear stating your eligibility
excel vba go to statement
excel vba go to statement1
VBA Excel Go to Statement

Related Topics

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

Excel VBA IsArray Function

VBA IsArray Function: The IsArray function in VBA returns a Boolean, showing whether the given variable is an Array or not. Syntax IsArray (VarName) Parameter VarName (required)- This parameter represents the variable that you want...

1 minute read.

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Excel VBA WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Excel VBA Sin Function

VBA Sin Function: The Sin function in VBA returns the sine value for a supplied angle. Syntax Sin (Number) Parameter Number (required) –This parameter represents the angle (in radians) to calculate the sine value. Return This function...

1 minute read.

Basics of Userform

A User Form is a built-in customized dialog box that fetches data from the user through a user-friendly dialog box or window that makes up part of an application's user interface. It...

9 minutes read.

Excel VBA InStr Function

VBA InStr Function The InStr function in VBA searches for a substring inside the given string. It returns the position of a substring within a string, as an integer if the substring is found...

1 minute read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

Excel VBA Tutorial

What is VBA? Introduction to Excel VBA: Visual Basic for Applications (VBA) is a programming language developed by Microsoft to automate operations in applications, such as Excel, Word, PowerPoint, etc. It...

5 minutes read.

Excel VBA: DO WHILE….Loop

DO WHILE….Loop The “Do While” Loop is the same, unlike the FOR statement, just that it will keep on looping till the specified condition is true. It is used when we want to...

3 minutes read.

Excel VBA UBound Function

Excel VBA UBound Function: The UBound function in VBA returns the highest subscript for the specified dimension in the given array. Syntax UBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

VBA Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.

Excel VBA InStrRev Function

VBA InStrRev Function: The InStrRev function in Excel VBA returns an integer representing the position of a substring within the specified string if the substring is fount else it returns...

2 minutes read.

Excel VBA DateDiff Function

The DateDiff function in VBA returns a Long data value representing the number of intervals between two specified dates/times where the type of interval is supplied by the user. Syntax DateDiff (Interval, Date1, Date2, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required)...

2 minutes read.

VBA VBScript Regex Methods Regex

VBA VBScript Regex Methods Regex The VBA Regex supports 3 methods which are as follows: ExecuteReplaceText Execute The execute method is used to extract a match from the given based on the defined matching...

3 minutes read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Excel VBA- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position...

4 minutes read.

Excel VBA Day Function

The function Day in VBA returns the day number (from 1 to 31) for the given date value. Syntax Day (Date) Parameter Date (required) – This parameter represents the date. Return This function returns the day...

1 minute read.