Excel VBA Goto command

Robertson1995

Board Regular
Joined
Apr 1, 2009
Messages
117
I have an excel VBA question about the Goto command. I have a sub that enters data based on a series of inputboxes and then the last command is to print the spreadsheet.

I also have an answer msgbox that vbYes continues to the next inputbox & vbNo exits sub. Instead of vbNo exiting sub, I would like it to goto the print commmand.

In VBA how do I use the goto command to go to a specific command at another place on the spreadsheet?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

ShawnPCooke

Board Regular
Joined
May 14, 2007
Messages
151
In order to use GoTo to do what you're suggesting, you would need to create a label at the place where you want the program to go. For example:

Code:
LabelName:
command 1
command 2

Then, to direct the program to that label, you would use the GoTo statement.

Code:
GoTo LabelName

The more important question is why you would use the GoTo statement. I'm assuming you're using a regular old If statement to direct the program flow from your MsgBox. There's no reason you can't include the code for printing the program there, or in another subroutine, and avoid GoTo altogether.
 
Upvote 0

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,197
It's very rare that you'd need to use goto and it's an age old coders mantra that you avoid it. Since we're all about best practice, publish the code and maybe there's a better way.
 
Upvote 0

hminney

New Member
Joined
Jun 15, 2014
Messages
2
It's very rare that you'd need to use goto and it's an age old coders mantra that you avoid it. Since we're all about best practice, publish the code and maybe there's a better way.
I use GOTO to skip code that is still in development rather than commenting,
eg I'm putting in Conditional Formatting, it's half written, and rather than commenting each line, I GOTO round it
but GOTO can only be used in linear code
 
Upvote 0

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
15,014
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I use GOTO to skip code that is still in development rather than commenting,
eg I'm putting in Conditional Formatting, it's half written, and rather than commenting each line, I GOTO round it
but GOTO can only be used in linear code

You do realize that on the edit toolbar (View - Toolbars - Edit) in the VBE there are "Comment Block" and "Uncomment Block" buttons so you can select & comment multiple lines in one go (you can also customise another toolbar to have the buttons on)?
 
Last edited:
Upvote 0

6StringJazzer

Well-known Member
Joined
Jan 27, 2010
Messages
2,700
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Another way to skip development code that is better than GoTo:

Code:
If False Then
   ' Development code goes here
End If

Still another way that I often use:

Code:
Dim Development As Boolean
Development = True

If Development Then
   ' Development code goes here
End If
 
Upvote 0

Forum statistics

Threads
1,190,743
Messages
5,982,699
Members
439,790
Latest member
jonaust

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top