The Procedure Builder (ALT + P) quickly generates a procedure based on your desired settings and inserts that procedure directly into the active VBA code module.
Shortcut: ALT + P
This section will serve as an introduction to Procedures in VBA and contains a walk-through of AutoMacro's Procedure Builder.
The Procedure Builder allows you to define default settings that will appear each time you load the Procedure Builder. It's great if you're a beginner VBA user: research and set your settings once, and then use the Procedure Builder to generate all your procedures in the future. No more remembering what settings are needed!
This feature is located at the bottom right of the Procedure Builder:
When coding (VBA or any other language) typically code is stored in procedures. Procedures are just blocks of code that can be called (ran). You are probably familiar with the concept of recording a Macro in Excel. When you record a Macro, you're actually just creating a VBA procedure.There are two types of procedures in VBA: a Subroutine (Sub) and a Function. Subs and Functions have three primary differences:
Completed Functions and Subs look like this:
Because Functions return values, you must tell VBA what type of value to expect. Notice above we declare the Function to be a Boolean value (TRUE or FALSE):
Function Ex_Function() As Boolean
Instead, you can declare a Function as a "Variant" type which will accept all value types:
Function Ex_Function() As Variant
The Procedure Builder will set all Functions to be type Variant.
Toggle between Sub and Function here:
It's important to descriptively name procedures and add comments to describe what a procedure does. Your code may make sense to you now, but it may not make sense to someone else (including your future self). It's best practice to add comments for every procedure you create.
Add Names and Comments Here:
By default, procedures are accessible from the Macro list (subs only), and can be called by any other procedure. The Private declaration hides the procedure from the Macro List and sets the procedure to be only accessible from the current code module (note: technically you can get around this by using Application.Run).
To make the Private Declaration, add Private before the procedure name:
Private Sub Ex_Sub
You can also declare entire modules as "private". By adding Private Module to the top of your code module:
When marking an entire module as private you hide all procedures from the Macro list, but they can be easily referenced from other modules.
Define Public or Private Here:
All applications (Excel, PowerPoint, Outlook, etc.) have application-level settings. Some of these settings are very important for VBA coding. Important: Application-level settings are independent of procedures. They will not manually "re-set" after a procedure finishes. Instead they must be manually changed again when desired.
The Procedure Builder allows you to set the following application-level settings:
Note: "Display Message Box After Complete" is not an application.setting, we will discuss it in the next section.
Disable ScreenUpdating - The screen will not refresh while the procedure runs. This can drastically cut down processing time.
Disable Automatic Calculations - This is an Excel setting that turns off Automatic Calculations. This can drastically speed up processing time.
Disable Alerts - Warning messages and prompts will not be shown to the user. Instead VBA will process the default action.
Disable Events - Events are "triggers" that can cause code to run (Exs. workbook open, sheet activate, cell change, etc.). If your workbook has events, you probably want to disable events while your code runs to prevent unintended behavior (including endless loops).
Message Box On Procedure Completion - For procedures that take more than a few seconds to run, it often makes sense to display a message box upon completion. This alerts the user that the procedure has finished running. For our clients, we generally set up message box alerts for all procedures that take longer than 15 seconds to run.
Error Handling - By default, VBA will stop executing code if it encounters an error and will prompt a user to "Debug" the code. This is desirable during testing/development. However, there are many scenarios where it's desirable to handle errors in other ways.
Error handling is defined at the procedure level. It's not an application-level setting. So any procedure that needs the non-default error handling needs an error handling declaration:
To restore the default error handling, enter the following:
On Error Goto 0
This is desirable during testing/development. However, there are many scenarios where it's desirable to handle errors in other ways.
Note: It's "best practice" to avoid intentionally allowing errors and using error handling to process them. Instead you should use smarter code to avoid generating VBA errors in the first place.
Procedure Builder: Error Handling:
Last modified 12 months ago.