EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Tips VBA GoTo
Secondary Sidebar
VBA Tips
  • VBA Tips
    • VBA RGB
    • VBA Web Scraping
    • VBA AutoFill
    • VBA GoTo
    • VBA Color Index
    • VBA Wait
    • VBA Paste
    • VBA Copy Paste
    • VBA Remove Duplicates
    • VBA Sleep
    • VBA Font Color
    • VBA PowerPoint
    • VBA Borders
    • VBA Pivot Table
    • VBA Save As
    • VBA Tutorial For Beginners
    • VBA Charts
    • VBA Dictionary
    • VBA Conditional Formatting
    • VBA Paste Values
    • VBA Pause
    • VBA Refresh Pivot Table
    • VBA Macros
    • VBA Examples
    • Programming in Excel
    • VBA SendKeys
    • VBA Save Workbook
    • VBA PasteSpecial
    • VBA Function in Excel
    • VBA Visual Basic Applications
    • VBA Return

VBA GoTo

By Ashwani JaiswalAshwani Jaiswal

VBA GoTo

Excel VBA GoTo Statement

VBA Goto Statement is used for overcoming the predicted errors while we add and create a huge code of lines in VBA. This function in VBA allows us to go with the complete code as per our prediction or assumptions. With the help Goto we can go to any specified code of line or location in VBA. There is two way of doing it which we will see in upcoming examples.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,088 ratings)

How to Use Excel VBA Goto Statement?

We will learn how to use Excel VBA Goto Statement with a few examples.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

You can download this VBA GoTo Excel Template here – VBA GoTo Excel Template

Example #1

The first way of using VBA Goto is by Application.Goto method. With the help of Application.Goto statement we can to any specific location, workbook or worksheet which is opened currently. This statement will look like as below.

VBA GoTo Syntax

  • [Reference]: This is nothing but a specified cell reference. If the reference is not provided by default it will take you to the last used cell range.
  • [Scroll]: This a logical statement of TRUE or FALSE. If the value is TRUE it will scroll through the window, if the value if FALSE it will not scroll through the window.

For this go to the VBA window and click on Module which is in Insert menu as shown below.

Insert Module

Now it will open a new Module. There write the Subcategory of a macro with the name of Goto as we are performing that code as shown below.

Code:

Sub VBAGoto()

End Sub

VBA GoTo Example 1-1

Now write Application.Goto to enable to application or place where we want to go.

Code:

Sub VBAGoto()

Application.Goto

End Sub

VBA GoTo Example 1-2

After that give Reference to any worksheet or workbook and range of cells. Here we have given the range of Cell B3 and Worksheets of named as “VBA Goto”.

Code:

Sub VBAGoto()

Application.Goto Reference:=Worksheets("VBA_Goto1").Range("B3"),

End Sub

VBA GoTo Example 1-3

Now for actually going to the mentioned cell we need to add Scroll argument which will directly take us to the specified cell. Now we will change the scroll argument from FALSE as shown below.

Code:

Sub VBAGoto()

Application.Goto Reference:=Worksheets("VBA_Goto1").Range("B3"), Scroll:=False

End Sub

VBA GoTo Example 1-4

After running the code using F5 key or manually, we will see cursor will get shifted to cell B3 without changing the orientation of the selected sheet as shown below.

Example 1-5

Now we will change Scroll argument from FALSE to TRUE.

Code:

Sub VBAGoto()

Application.Goto Reference:=Worksheets("VBA_Goto1").Range("B3"), Scroll:=True

End Sub

 Example 1-6

Initially, we are keeping the cursor at cell A5 as shown below. Now run the complete code using F5 key or manually. We will see the cursor which was at cell A5, is now scrolled up and shifted to cell B3 and the whole table has moved to the top level of sheet starting with the same cell which B3 as shown in below screenshot.

Example 1-7

Example #2

There is another way of using VBA Goto argument. Using Goto in VBA by this example, we can skip the argument which is causing an error. For this, insert a new module in VBA and start Subcategory with the name of argument used as shown below. You can use any other name.

Code:

Sub VBAGoto()

End Sub

VBA GoTo Example 2-1

For this, we will consider 3 integers X, Y, and Z by opening Sub category in VBA as shown below.

Code:

Sub VBAGoto()

Dim X As Integer, Y As Integer, Z As Integer

End Sub

VBA GoTo Example 2-2

Now also consider some mathematical division where we will divide 10, 20 and 30 with 0, 2 and 4 as shown below.

Code:

Sub VBAGoto()

Dim X As Integer, Y As Integer, Z As Integer

X = 10 / 0
Y = 20 / 2
Z = 30 / 4

End Sub

VBA GoTo Example 2-3

If we run the code we will get the same error message of Run-time error 11.

Example 2-4

Above error message Run-time error ‘11’ comes only when the written mathematical expression is incorrect. Now to overrule this error, we will use text On Error GoTo with word YResult to skip error message and get the output which works fine as shown below.

Example 2-5

Still, our code is not complete. Using Goto with statement “YResult:” will only skip the error line of code. But it will again show the error as Labe not defined as shown below.

Code:

Sub VBAGoto()

Dim X As Integer, Y As Integer, Z As Integer

On Error GoTo YResult:

X = 10 / 0
Y = 20 / 2
Z = 30 / 4

End Sub

VBA GoTo Example 2-6

Now to complete it, we need to define the Label. Label is the part of statement in VBA Coding, which is used when we want to skip a certain portion of code to any defined applicable line of code. As we already have YResult with Goto argument. Then we will insert the same just before integer Y. Now run the code again.

Code:

Sub VBAGoto()

Dim X As Integer, Y As Integer, Z As Integer

On Error GoTo YResult:

X = 10 / 0

YResult:

Y = 20 / 2

Z = 30 / 4

End Sub

VBA GoTo Example 2-7

As seen and done, we have not got any error message which means that our code is correct and it is skipping that line of code which was causing an error and giving the output where the correct code has been written. Now to print the result of code need to insert message boxes for each Integer with the help argument MsgBox as shown below.

Code:

Sub VBAGoto()

Dim X As Integer, Y As Integer, Z As Integer

On Error GoTo YResult:

X = 10 / 0

YResult:

Y = 20 / 2

Z = 30 / 4

MsgBox X
MsgBox Y
MsgBox Z

End Sub

VBA GoTo Example 2-8

Once done then run the complete code to see the output. We will the output of division of each defined integers as 0, 10 and 8 as shown in below screenshot as well.

Example 2-9

On Error GoTo YResult statement helped us to directly jump to mentioned result point integer as we did for integer Y. And the output for X as 0 shows that there was incorrect statement argument written. We can the Label even before Z but that would give us the result of Z integer only. For X and Y it will again show 0 as output.

Pros of VBA On Error

  • We can calculate any mathematical formula even if it is incorrect.
  • For bigger coding structures where there are chances or having an error, using GoTo may give correct result even among the line of codes.
  • This gives better result as compared to the result obtained from normal excel calculations in spite of having an incorrect argument.

Things To Remember

  • Remember to the file in Macro-Enabled Excel file so that we can use created VBA code many and multiple times.
  • Compile the written code before implementing with any excel requirement.
  • You can assign the written code to any button so that we can quickly click on the button and run the code.
  • Use Label as shown in example-2 appropriately so that we will get the result for the complete correct code.

Recommended Articles

This has been a guide to VBA GoTo Statement. Here we discussed how to use Excel VBA GoTo Statement along with some practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Loops
  2. VBA Function
  3. VBA VLOOKUP
  4. VBA On Error Goto
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more