Creating Sub and Function procedures
In this recipe, we will be creating the two most common types of procedures; Sub and Function procedures.
A Sub procedure is, in essence, a series of VBA statements, with the purpose of performing an action within Excel. The simplest Sub procedures can be as short as a single line of code (between the Sub and End Sub statements), while complex Sub procedures can become quite long. It is, however, advisable to divide huge Sub procedures into a series of shorter Sub procedures.
Another procedure often used in VBA is the Function procedure. The purpose and use of this type of procedure differs from the Sub procedure in the sense that it performs a calculation and returns a single value.
It can happen that you want to do a complex calculation, like a nested If
statement, or a nested And
/Or
statement. By incorporating these functions into a User-Defined Function (UDF), you can simplify the process considerably.
Function procedures can also...