Locating Builtin Functions

VBA comes with many built-in functions. These functions can be looked up easily in the Visual Basic online help. To access an alphabetical listing of all VBA functions, choose Help | Microsoft Visual Basic Help in the Visual Basic Editor window. On the Contents tab, open the Visual Basic Language Reference folder. Then click Functions.

Take, for example, the MsgBox or the InputBox function. One of the features of a good program is its interaction with the user. When you work with Microsoft Excel, you interact with the application by using various dialog boxes. When you make a mistake, a dialog box comes up and displays a message informing you of the error. When you write your own procedures, you can also inform the users about an unexpected error or the result of a specific calculation. Do this with the help of the MsgBox function. So far you have seen a simple implementation of this function. In the next section, you will find out how to control the looks of your message. You will also learn how to get information from the user with the InputBox function. But before we go on to discuss these functions in detail, let's take a look at one VBA function that can be especially useful to you now that you have familiarized yourself with variables and their data types.

Visual Basic has the VarType function that returns an integer indicating the type of a variable. Figure 4-4 displays the VarType function's syntax and the values it returns.

$ Microsoft Visual Basic Help

Contents j Answer Wizafdj index ]

^ Sb$ Function

?) SteCorrp Function

StoCorrv Function

?1 SliReverse Function

2 Siting Function

?) Stfing$ Function

?] Switch Fuidion

SYD Function

Tab Function

?] T an Fuidion

?1 Time Function

Time$ Function

?] Timer Function

?] TimeS eriai Function

?] TimeValue Function

?) Type Conversion Fund

?] Tnm Function

?1 T lirnt Function

7] TjipeN ame Function

f] U8ound Function

23 UCase Findion

?] UCa$e$ Function

Val Function

2Ü VarType Function

^ Weekday Function

Weekday ame Functio

Year Function

EE! & Groups

M 1 *

See Also Example Specifics

Returns an Integer indicating the subtype of a variable. Syntax

The required isurname argument is a Variant containing any variable except a variable of a user-defined type.

Return Values

Constant vbEmpty vbNull wblnteger vbLong vbs ingle vbDouble vbCurrency vbDate vbStrlng vbObject vbError

Value Description

0 Empty (uninitialized)

1 Null (no valid data)

2 Integer

3 Long integer

4 Single-precision floatingpoint number

5 Double-precision floatingpoint number

Currency value Date value String Object Error value zi

Figure 4-4: With the built-in VarType function, you can tell the data type the variable holds.

Now, let's see how you can use this function in the Immediate window.

1. Open the Immediate window.

2. Type the following statements that assign values to variables: age = 18

birthdate = #1/1/1981# firstName = "John"

3. Now ask Visual Basic what type of data each of the variables hold: ?varType(age)

When you press Enter, Visual Basic returns 2. As shown in Figure 4-4, the number 2 represents the Integer data type.

?varType(birthdate)

Visual Basic returns 7 for Date. If you make a mistake in the variable name (let's say you type birthday instead of birthdate), Visual Basic returns zero (0).

?varType(firstName)

0 0

Post a comment