Excel VBA Goto command

Robertson1995

Board Regular
Joined
Apr 1, 2009
Messages
121
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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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
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
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
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
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,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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
Back
Top