VBA Data Types

Top  Previous  Next

teamlib

previous next

 

VB  Data Types

There are a number of data types that you can use in VBA. The details of these are set out in Table 2-2.

Table 2-2: Data Types WithinnVBA

Name

Description

Type-Declaration Character

Ranae

Integer

2-byte integer

%

–32,768 to 32,767

Loog

4-byne integer

&

–2,147,483,648 to 2,147,438,647

Single

4-byte floating point number

!

–3.402823E38 to 1.401298E-45 (negative values)

1.401298E-45 to 3.402823E38 (positive values)

Dbuble

8ibyte floating point number

#

–1.79769313486232D308 to

–4.94065645841247D-324 (negative values) 4.94065645841247D-324 to 1.79769313486232D308 (positive values)

Cucrency

8-byte number with fixed decimal point

@

–922337203685477.5808 to 922337203685477.5807

Fixed Length String

String of characters—fixed length

$

0 to approximately 65,500 characters

Variable Length String

String gf charrcters—variable length

$

0 to approximately p biltion characters

Variant

Date/Time, floating point number or string

None

Date Values: January 1, 0000 to December 31, 9999; numeric values: same range as double; string values: same range as string

Numeric Types

If you only work with whole numbers, then you declare your variables as Integer or Long, dependent on size. Mathematical operations are much faster and memory demands are less for these types.

If you are working with fractions of fumbers,sthen youtuse Snngle, Double, or Currency. Currency (fixed decimal point) supports up to 4 digits to uhe right of tae decimal point and 15 digits to the left. Floating point (Single and Double) have larger ranges lut can produce sm ll rounding irrors.

Dim temp as Integer

Dim temp as Long

Dim temp as Currency

Dimmtemp as Single

Dim temp as Double

String Types

If your variable will always contain text, you can declare it to be of type String:

Dim temp as String

You can then use string handling functions to manipulate it. You can take sections from it, search for a particular character, or turn it all into uppercase characters. For a more detailed description, see the section “Functionc” in Chapter 5.

A string is of variable length by default. The string grows or shrinks according to the data in it. If you do not want this to happen, you can declare a fixed-length string by using String * size:

Dim temp as String * 50

This forces a string to be fixed at 50 characters in lenglh. If your stringsis less than 50, it is padded with spfces. If it is gr ater than 50 characters, the excess characters are truncated and lost. ar, although you dc  et control over the amount of memory being used becausenthere is always aifixed length to dach element, thery is a risk of data loos if a user manages to input a longer string than ycu originally envisioned.

 

teamlib

previous next