×

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

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 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 CCur Function

The CCur function in VBA is used to convert an expression into a Currency data type. It can take a maximum of 15 digits to the left of the decimal place and...

1 minute read.

VBA Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

Excel VBA Str Function

VBA Str Function: The Str function in VBA converts the given number into a string representation of that number. Syntax Str (Number) Parameter Number (required) – This parameter represents the numeric value that you want...

1 minute read.

Excel VBA CStr Function

VBA CStr Function: The CStr function in VBA converts an expression into a string data type. Syntax CStr (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA LTrim Function

VBA LTrim Function: The LTrim function in VBA removes the leading spaces from a supplied text string. Syntax LTrim (String) Parameter String (required) - This parameter represents he text string that you want to remove...

1 minute read.

Excel VBA Asc Function

VBA Asc Function: The Asc function in VBA returns an integer showing the character code for the first character of a given string. Syntax Asc (String) Parameter String (required) – This parameter represents the text...

1 minute read.

Excel VBA DateValue Function

The DateValue function in VBA returns a VBA Date from the given String representation of a date wherein the time information is ignored. It is unable to interpret dates that include the...

1 minute read.

Excel VBA Trim Function

The Trim function in VBA removes the leading and trailing spaces from the specified string. Syntax Trim (String) Parameter String (required) – This parameter represents the string from which you want to remove the leading and...

1 minute read.

VBA Regex Pattern Validation

VBA Regex Pattern Validation The key purpose of using Regex in VBA was to validate the data and extract the same category of data. There are certain formats that are standard...

6 minutes read.

Excel VBA Year Function

Excel VBA Year Function: The Year function in VBA returns the four-digit year for the specified date. Syntax Year (Date) Parameter Date (required) – This parameter represents the significant date. Return This function returns the four-digit year...

1 minute read.

VBA runtime error 1004

What is 1004 error?  VBA 1004 Error, also known as object-defined or application-defined, is a runtime error in VBA, usually, if the specified range does not exist in the worksheet or if the Application...

6 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Excel VBA Hour Function

The hour function in VBA returns the hour element for the specified time. Syntax Hour (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the hour element for the specified time. Example 1 Sub HourFunction_Example1() ...

1 minute read.