LESSON SUMMARY: The basic building block of a VBA program is the Procedure . In this lesson we will be looking mainly at the procedure.
NOTE:This lesson is the MOST IMPORTANT one in learning how to use VBA!
Now that you have a understanding of the ' VBA BASIC CONCEPT ' introduced in 'Lesson 1', work through 'Lesson 2' carefully and slowly to see how this works in practice.
Do not worry about trying to understand all of the 'small' details yet, such as the meaning of 'vbYesNo' or 'Dim'. Concentrate instead on the 'bigger' picture, the 'whole' stucture of a 'procedure'.
Have patience. If you do not understand something the first time, go back through the information presented again or use the online help in the 'VB Editor'.
At the end of these first two lessons, you should have a good grasp of the basic concepts of computer programming, of 'objects' and their 'properties', and how 'Procedures', 'Variables', 'Operators' and 'Functions' work together to create a 'VBA project'.
EXERCISE:
STEP 1: Open WORD and create a new document called 'my_first_program.doc' and save it into your working directory.
STEP 2: Select the Toolbox button from the Visual Basic Toolbar , then select a CommandButton from the 'Toolbox' dialog.
The CommandButton will be automatically pasted into your document. You can move it to where you want.

STEP 3: Double click on the CommandButton in design mode to enter the 'VB Editor'.
You will see ....
Private Sub CommandButton1_Click()
End Sub
STEP 4: Copy the following and paste it in between the two lines above...
Dim Response as string
'Declare a variable
Response = InputBox("What is your name?")
'Set variable to user input using the InputBox function
Response = "Your name is " & Response
'Use the & operator to create a user friendly text string
MsgBox Response
'display the value of the variable using the MsgBox function
STEP 5: Exit the 'VB Editor', turn off Design Mode , then click on the CommandButton. An 'input' dialog box should appear asking "What is your name?". After your enter your name and close the dialog box, another dialog box will appear telling you what your name is.
STEP 6: When you have it working, turn on Design Mode and right click on the CommandButton and select alter some of the script, such as the text of the messages displayed.
EXPLANATION:
To work, a VBA procedure has three parts, Variables, Operators, and Functions.
In our example above, ' Response ' was a variable, ' & ' was an operator and ' InputBox ' and ' MsgBox ' were functions.
The code above, in red, will be black in the VBE. Red is used here to stand out from the normal text.
The code above, in green, and preceeded with a " ' ", is a comment line. It will still be green in the VBE.
A comment line is used to explain the code, so when you come back at a later date, you know what you were originally trying to do.
In the following lessons I have tried to tread the fine line between trying to keep the code simple, but at the same time provide enough comments to explain the code. I hope the balance is right for you.
VARIABLES:
Variables are names representing data. They are declared within a procedure using the Dim statement, as shown in the script above. Variables can be declared as one of the following data types: Boolean, Byte, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, or Variant.
NOTE: If you do not specify a data type, the 'variant' data type is assigned by default. For a start you DO NOT need to worry about declaring 'variables' so you can concentrate more on the structure of using VBA. However, you will eventually need to declare your 'variables' to produce 'good' code.
[ For more information on data types, search for 'data type summary' in the online help in the 'VB Editor'.]
Once declared the variables can be ..
set eg. ' Response = InputBox("What is your name?") '
altered eg. ' Your name is " & Response ' , or
used eg. ' MsgBox Response ' .
NOTE: To display a number as text, a string, it must be converted to a String by the 'Str' function. To do the opposite, to convert a string to a number, use the 'val' function. Variables declared using Dim within a procedure, as above, are only recognised within that procedure.
OPERATORS:
Operators act on variables to achieve a result, and can be thought as the 'glue', in that they hold all the other elements in VBA code together. In the code above the '+' operator is used to add '!' to the 'Msg' variable.
There are four types of operators.
- Arithmetic Operators
The arithmetic operators are '+', '-', '/', '*', '^' and 'mod'. [Used on numbers only]
- Comparison Operators
The comparison operators are '<', '<=' ,'>', '>=', '=', and '<>' [Used on numbers and strings]
Example Code ....
Var1 = 3
Var2 = 6
'is less than
If Var1 < 5 Then
MsgBox "Var1 = " & Var1
MsgBox Var1 & " is less than 5!"
End If
'is greater than
If Var2 > 5 Then
MsgBox "Var2 = " & Var2
MsgBox Var2 & " is greater than 5!"
End If
'is equal to
If Var2 = 6 Then
MsgBox "Var2 = " & Var2
MsgBox Var2 & " is equal to 6!"
End If
'is not equal to
If Var1 <> 5 Then
MsgBox "Var1 = " & Var1
MsgBox Var1 & " does not equal 5!"
End If
- Concatenation Operators
The concatenation operators are '&' and '+'. [Used on strings only]
- Logical Operators
The main logical operators are 'And', 'Eqv', 'Imp', 'Not', 'Or' and 'Xor' [Used on numbers and strings]
NOTE: When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. If you are not sure what this order is, look up 'operator precedence' in the online help in the VB Editor.
FUNCTIONS:
Functions 'do something', generally returning a value.
They can be ....
inbuilt, such as the 'MsgBox', 'Str', and 'Val' functions, or ...
a coded 'procedure' called a 'Function' that's declared as Public, as the PlaySound function has been in 'VBA_Exercise3.doc'.
Note: Functions will be looked at in more detail in 'Lesson 13' in VBATutor Part Two.
SUMMARY:
'Values', either input from the user or 'properties' of WORD 'objects', are put into 'variables'. Then, 'operators', and 'functions' will be used to act upon those 'variables'.
|