Introduction to Procedures |
|
Procedures |
A procedure is a section of code created to carry an assignment, separate from a spreadsheet, whose action can be used to complement a spreasheet. You create the procedure by writing code. One of the advantages of a procedure is that, once it exists, you can access it when necessary and as many times as you want. There are two categories of procedures you will use in your spreadsheets: those that are already installed with Microsoft Excel and those you will create. |
In the Visual Basic language, like most other languages, there are two types of procedures: functions and sub procedures. A sub procedure is an assignment that is carried but does not give back a result. To create a sub procedure, start with the Sub keyword followed by a name (like everything else, a procedure must have a name). The name of a procedure is always followed by parentheses. At the end of the sub procedure, you must type End Sub. Therefore, the primary formula to create a sub procedure is: |
Sub ProcedureName() End Sub The name of a procedure should follow the same rules we learned to name the variables. In addition:
The section between the Sub and the End Sub lines is referred to as the body of the procedure. Here is an example: Sub CreateCustomer() End Sub In the body of the procedure, you carry the assignment of the procedure. It is also said that you define the procedure or you implement the procedure. One of the actions you can in the body of a procedure consists of declaring a variable. There is no restriction on the type of variable you can declare in a procedure. Here is an example: Sub CreateCustomer() Dim strFullName As String End Sub In the same way, you can declare as many variables as you need inside of a procedure. The actions you perform inside of a procedure depend on what you are trying to accomplish. For example, a procedure can simply be used to create a string. The above procedure can be changed as follows: Sub CreateCustomer() Dim strFullName As String strFullName = "Paul Bertrand Yamaguchi" End Sub |
|
||
Home | Copyright © 2008-2016, FunctionX, Inc. | Next |
|