... is a pain with VBA when developing and running code from within the VBE because you need to:
Navigate from the code that you are editing to the Sub or Function that you want to run ... which can be elsewhere in the same Module or in a different Module entirely
Run the code (ie using F5 or the Run > Run Macro menu option)
Navigate back from the Sub or Function that you ran back to the code that you were editing ... which (again) can be elsewhere in the same Module or in a different Module entirely
Sometimes you can need to do this again, and again, and again ... it becomes a bit frustrating. So I came up with a solution using VBE_Extras ... two in fact.
Sub To Run
Navigate to the Sub (or Function ... so long as it takes no parameters and is in a standard Module) that you will want to run (again and again ...). In the VBE menu, select Extras > Sub To Run > Set Sub To Run (or press Shift + F6 if you haven't changed the default keyboard shortcuts).
Navigate back to where you want to edit your code (in fact, navigate to anywhere within the same VBA project).
When you want to run the 'Sub To Run', in the VBE menu, select Extras > Sub To Run > Run Sub To Run (or press F6 if you haven't changed the default keyboard shortcuts).
The Sub (or Function) that you designated as the Sub To Run will run. That's right, not the Sub (or Function) that your cursor is at.
Run Again
Similar to Sub To Run but instead of manually selecting the Sub To Run, VBE_Extras automatically remembers which Sub (or Function) you last ran with F5 or the Run > Run Macro menu option. All you need to do is, in the VBE menu, select Extras > Sub To Run > Run Again (or press F10 if you haven't changed the default keyboard shortcuts) and the Sub (or Function) that you last ran will run again.
Great. But is that it?
No. How about automatically clearing (or scrolling to the end of) the Immediate window:
When you use Sub To Run
When you use Run Again
When you use the VBE's Run > Run Macro menu option or press F5
In the Run tab of the VBE_Extras Settings dialog, you can switch on or off options for each of these. No more 'mangled text' in the Immediate window (you know, when you left the cursor in the middle of a line and the VBE sends all the new Immediate window text to the middle of that line ... what were Microsoft thinking?!)
What else should I know?
These options only work when you run code from within VBE (e.g. not if you run code from the user-interface) and not when you use F8 to step through code.
The options to clear (or scroll to the end of) the Immediate window can be switched off in the Settings, as can Run Again (Sub To Run can't be / doesn't need to be switched-off as such ... it is 'passive', in other words if you don't actively use it then it is effectively switched off hence no Setting to switch it off specifically).
Sub To Run and Run Again do not work in Outlook and, due to a quirk of how Word works, you may see the screen flicker when using Sub To Run and/or Run Again in Word.
The VBA project that you want to run code within needs to have been saved at least once (not that it needs to have been saved immediately before using Sub To Run or Run Again ... it just needs to have been saved at least once so that the associated Workbook / Document / Database / Presentation has a valid filename).
And anything else?
You can clear the Immediate window (or scroll to the end or start of it) using VBE_Extras menu options or keyboard shortcuts at any time, not just when using Sub To Run or Run Again ... see my How do YOU clear the Immediate window in the VBE? post for details.
VBE_Extras provides many other ways to simplify navigating around your VBA project in addition to Sub To Run and Run Again, for example:
Identify, and jump to, places that particular declarations (variables, procedures, Modules ... anything you can declare in VBA code) are referenced (i.e. the name of the declaration is used) within your VBA project †- see the Working with declarations and references video
Do the reverse and jump to the declaration from any use of the declarations name - see the same video as above
Navigate code using the mouse - see my Using mouse clicks to navigate VBA code in the VBE post for details
VBE_Extras automatically tracks your cursor location history so that you can move back (and forth) to places that you previously viewed or edited code - see my Tracking your cursor location history in the VBE post for details
†If your VBA project has "project references" to other VBA projects then places that the declaration is used within those other VBA projects are included
Comments