Other topics about Operation Basics
This section provides descriptive information on the user interface elements of a code window that lets you code a VBA program, and walks through a sample program (procedure) that finds the maximum value contained in an array so you can gain insight into how to create your own programs.
A code window is where you code a VBA program. When you are working with a user form, you can open the code window for that user form by double-clicking a control (such as a button or text box) placed on the form. Similarly, when you are working with a standard or class module, you can open the code window associated with that module by double-clicking the module's icon in Project Explorer.
Code window for a standard module
Provides a list of objects currently used within the code window.
Provides a list of procedures that reside within the code window. When you are working with a user form, this provides a list of events (actions such as click or double-click).
Primarily intended for use when debugging a program.
Displays only the procedure at the cursor position.
Displays the entire program contained in the code window.
This section walks through a sample program that finds the maximum value contained in an array while breaking down the code into a number of blocks and describing what they do. Line numbers are added for description purpose only, and do not appear in the actual program source code.
Sample program that finds the maximum value contained in an array
10| Option Explicit
20|
30| Sub Maximum()
40|
50| Dim q As Variant
60| Dim x(100) As Integer
70| Dim i As Integer, n As Integer
80| Dim Start As Integer, Last As Integer, Num As Integer
90| Dim Maximum As Integer
100|
110| ' Defining the array
120| q = Array(7, -2, 3, -20, 15, -6, 27, -12, 9, -5, 18, 23, _
130| 9, -16, 22, 0)
140|
150| Start = LBound(q)
160| Last = UBound(q)
170| Num = Last - Start + 1
180|
190| For i = Start To Last
200| x(i) = q(i)
210| Next i
220|
230| Maximum = x(Start)
240|
250| For n = Start + 1 To Last
260| If x(n) > Maximum Then Maximum = x(n)
270| Next n
280|
290| MsgBox Maximum
300|
310| End Sub
Let us break down the code into a number of blocks and see what they do.
This instruction mandates explicit declaration of variables.
The code enclosed between Sub Maximum() and End Sub will be executed within the E5071C's macro environment. Thus enclosed code is called a procedure. In this example, "Maximum" is the procedure name.
These lines declare data types of variables using Dim statements. A statement is the minimum instruction unit based on the syntax. The sample program declares the variable "q" as Variant, and the variables "x(100)", "i", "n", "Start", "Last", "Num", and "Maximum" as Integer. For a complete list of statements and data types supported by VBA, see VBA Online Help.
Any text preceded by a comment indicator (') is treated as a comment.
These lines use VBA's Array function to initialize the array. The q() array contains elements delimited with commas in the ascending order of index numbers (zero-based). A combination of a space and underscore (_) is used to continue the statement across two or more lines.
Stores the starting index number of the q array into the Start variable.
Stores the last index number of the q array into the Last variable.
Stores the number of elements in the q array into the Num variable.
The code within each For ...Next statement is iterated until the counter reaches the specific number.
Stores the contents of the q array (Variant) into the x variable (Integer).
Uses the first element of the x array as the tentative maximum value.
Compares the tentative maximum value with each of elements that follow; if an element is larger than the tentative maximum value, then that element is used as the tentative maximum value.
Uses a message box function to display the maximum value. For a complete list of functions supported by VBA, see VBA Online Help.
The above sample program consists of a single procedure contained in a single module. However, when you deal with procedures and variables across multiple modules, you should be aware of the scope of variables and procedures.
When you use COM objects in Visual Basic Editor, the editor's auto-complete feature allows you to easily type in keywords without misspelling them.
The following procedure assumes that you are entering the SCPI.INITiate(Ch).CONTinuous object.
In a standard module, type sub main and press the Enter key. End Sub is automatically added.
Typing scpi followed by a dot (.) brings up a list of classes under the SCPI class.
Typing in automatically moves focus to INITiate in the list box.
Typing ( brings up a list of indexes.
Typing 1). brings up a list of classes under the INITiate class.
Typing c automatically moves focus to CONTinuous in the list box.
Typing = brings up a list box for setting a Boolean value (True/False).
Typing t automatically moves focus to True.
Pressing the Enter key completes the statement: SCPI.INITiate(1).CONTinuous = True.