Coding a VBA Program

Other topics about Operation Basics

Overview

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.

User Interface Elements of a Code Window

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

1. Object box

Provides a list of objects currently used within the code window.

2. Procedure box

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).

3. Margin indicator bar

Primarily intended for use when debugging a program.

4. Show Procedure button

Displays only the procedure at the cursor position.

5. Show Module button

Displays the entire program contained in the code window.

Creating a Simple VBA Program

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.

Line 10

This instruction mandates explicit declaration of variables.

Lines 30 to 310

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.

Lines 50 to 90

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.

Line 110

Any text preceded by a comment indicator (') is treated as a comment.

Lines 120 to 130

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.

Line 150

Stores the starting index number of the q array into the Start variable.

Line 160

Stores the last index number of the q array into the Last variable.

Line 170

Stores the number of elements in the q array into the Num variable.

Lines 190 to 210 and Lines 250 to 270

The code within each For ...Next statement is iterated until the counter reaches the specific number.

Line 200

Stores the contents of the q array (Variant) into the x variable (Integer).

Line 230

Uses the first element of the x array as the tentative maximum value.

Line 260

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.

Line 290

Uses a message box function to display the maximum value. For a complete list of functions supported by VBA, see VBA Online Help.

 

Auto-complete Feature

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.

  1. In a standard module, type sub main and press the Enter key. End Sub is automatically added.

  2. Typing scpi followed by a dot (.) brings up a list of classes under the SCPI class.

  3. Typing in automatically moves focus to INITiate in the list box.

  4. Typing ( brings up a list of indexes.

  5.  Typing 1). brings up a list of classes under the INITiate class.

  6.  Typing c automatically moves focus to CONTinuous in the list box.

  7. Typing = brings up a list box for setting a Boolean value (True/False).

  8. Typing t automatically moves focus to True.

  9. Pressing the Enter key completes the statement: SCPI.INITiate(1).CONTinuous = True.