Understanding Subroutines



A subroutine (usually called a sub) is a set of programming statements that is executed as a unit by the VBA engine.
Components of the subroutine:
-Declaration: All procedures must be declared so that VB knows where to find them.
The name assigned to the procedure must be unique within the VB project
The sub keyword identifies this procedure as a subroutine.
-Terminator: All procedures must be terminated with the End keyword followed by the type of procedure that is ending
-Declarations area: Although variables and constants can be declared within the body of the procedure , good programming conventions require variables to be declared near the top of the procedure where they'll be easy to find
-Statements: A VBA procedure can contain many statements. Very large subroutines can be difficult to work with.
Instead of adding too many features and operations in a single procedure, place operations in separate procedures and call those procedures when those operations are needed
-Call to another procedure: The other procedure is anther sub but could have been a function as well
The called procedure can be in any module within the current project