|

How to Understand Visual Basic Syntax

This is an excerpt from Microsoft Excel Visual Basic Help, with extra Notes.

The syntax in a Visual Basic Help topic for a method, statement, or function shows all the elements needed to use the method, statement, or function. The examples in this topic explain how to interpret the most common syntax elements.

1.        Activate Method Syntax

object.Activate

In the Activate method syntax, the italic word “object” is a placeholder for information you supply — in this case, code that returns an object.  Type the words that are bold exactly as they appear.  For example, the following procedure activates the second window in the active document.

Sub MakeActive()
    Windows(2).Activate
End Sub

2.        MsgBox Function Syntax

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

In the MsgBox function syntax, the italic words are the named arguments of the function. Arguments enclosed in brackets are optional. (Do not type the brackets in your Visual Basic code.) For the MsgBox function, the only argument you must provide is the text for the prompt.

You can specify arguments for functions and methods in code either by position or by name. To specify by position, follow the order given in the syntax, separating arguments with commas.  For example:

MsgBox "Your answer is correct!", vbInformation, "Answer Box"

To specify an argument by name, use the argument name followed by a colon and an equal sign (:=), and the argument’s value. You can specify named arguments in any order, for example:

MsgBox Title:="Answer Box", Prompt:="Your answer is correct!"

The syntax for functions and some methods shows the arguments enclosed in parentheses. These functions and methods return values.  So, you must enclose the arguments in parentheses to assign the value to a variable. If you ignore the return value or if you don’t pass arguments at all, don’t include the parentheses. Methods that don’t return values do not need their arguments enclosed in parentheses. These guidelines apply whether you’re using positional arguments or named arguments.

In the following example, the return value from the MsgBox function is a number.  This indicates the selected button value, stored in the variable myVar. Because we use the return value, parentheses are necessary. Another message box then displays the value of the variable.

Sub Question()
  Dim myVar As Integer
  myVar = MsgBox(Prompt:="I enjoy my job.", _
    Title:="Answer Box", Buttons:=4)
  MsgBox myVar
End Sub

3.        Notes on MsgBox Function Syntax

  1. Using Option Explicit is highly recommended.  But then, the code given by Microsoft for Sub Question above will neither compile nor run.  You need the extra line above, Dim myVar As Integer, to define the variable myVar.
  2. The MsgBox code above will be more understandable if you substitute built-in constants for the “buttons” values.  Buttons:=4 above makes the MsgBox show the Yes and No buttons.  It makes more sense as Buttons:=vbYesNo.  Make sure you have Tools > Options > Editor > Auto List Members turned on.  Then, when you get to the second MsgBox parameter, Visual Basic will give you a list of the options.
  3. Likewise, the line MsgBox myVar is not particularly understandable, returning 6 or 7.  But when you know that these values have the built-in constants vbYes and vbNo, it makes more sense.

4.        Option Compare Statement Syntax

Option Compare {Binary | Text | Database}

In the Option Compare statement syntax, the braces and vertical bar denote a mandatory choice between three items. (Do not type the braces in the Visual Basic statement). For example, the following statement specifies that in this module, strings will be compared in an order that is not case-sensitive.

Option Compare Text

5.        Dim Statement Syntax

Dim varname[([subscripts])] [As type] [, varname[([subscripts])] [As type]] . . .

In the Dim statement syntax, the word Dim is a required keyword. The only required element is varname (the variable name). For example, the following statement creates three variables: myVar, nextVar, and thirdVar. These are automatically declared as Variant variables.

Dim myVar, nextVar, thirdVar

The following example declares a variable as a String. Including a data type saves memory and can help you avoid errors in your code.

Dim myAnswer As String

To declare several variables in one statement, include the data type for each variable. Variables declared without a data type are Variants.

Dim x As Integer, y As Integer, z As Integer

In the following statement, x and y are assigned the Variant data type. Only z is assigned the Integer data type.

Dim x, y, z As Integer

(Note: In VB.NET, with the same statement, x, y, and z would all be Integers)

If you are declaring an array variable, you must include parentheses. The subscripts are optional. The following statement dimensions a dynamic array, myArray.

Dim myArray()

(Note: As mentioned above, without a type declaration, this array will be a Variant, a data type best avoided  Variants do not exist in VB.NET.)

6.        Notes on Dim Statement Syntax

Instead of “As Integer” or “As String”, for some variable types you can use a type declaration suffix (ending).  These are:

SuffixVariable TypeExamples
%Integer (index or counter)I%, Cnt%
&Long IntegerLo&
!Single (Real Number)MyPay!
#Double (Real Number)Precise#
@CurrencyCost@
$StringName1$

We covered this in detail in this earlier post on variable types.

The great thing about type-declaration suffixes is that when you see one, you can be absolutely sure of the type of that variable.  In contrast, the suggested prefixes (as mentioned in this post) are merely conventions.  Following that convention, a variable named daBirth should be a date but, is it really?  To be sure, you would have to see its Dim statement (press Shift+F2).  However, a variable named Name1$ will always, without a shadow of a doubt, be a string.

Similar Posts