Specifications

Chapter 13 227
Use of Macros
Making/Editing Macros
13. Use of Macro
Structure of macro
Each part of a macro is explained by using the following example of a simple macro. The
parts of the program are indicated by the numbers to the right in parentheses (these
numbers are not used in the actual program).
Example 13-1 Example of macro program
Sub Sample1() (1)
' Sample Program 1 (2)
Dim i As Integer (3)
Dim total As Integer (3)
total = 0
For i = 1 To 10 Step 1 (4)
total = total + i (5)
Next i (4)
MsgBox ("Total = " & Val(total)) (6)
End Sub
Each part of the above macro is explained as follows.
1. The macro starts with the form of “Sub Sample1()” and ends with “End Sub”. This
group is called the procedure. Here, “Sample1” is a procedure name.
2. Everything written to the right of the comment symbol (') is a comment.
3. The type of parameter is declared by a Dim statement. Here, a command is called a
statement. In the program example, the variables of “i” and “total” are declared by the
type of integer. For more on statements prepared by E4991A VBA and the types of
parameters handled in E4991A VBA, please refer to E4991A VBA help.
4. By using a For...Next statement, a loop is executed a fixed number of times.
5. “total” plus “i” equals “Total”. Here, since “i” is used as the fixed number of times in a
For...Next statement, the numeric values from 1 to 10 are added to “total” in order.
6. The result of the calculations can be displayed by using the message box function. For
more on the functions provided with E4991A VBA, please refer to E4991A VBA help.
NOTE In Example 13-1, only a single procedure is explained as an example. The larger a macro
gets, the more care needs to be taken in programming, such as when selecting the declaring
method for dealing with a parameter between multiple procedures.