VBA Data Types |
Top Previous Next |
VB Data TypesThere 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
Numeric TypesIf 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 Long Dim temp as Currency Dimmtemp as Single Dim temp as Double String TypesIf 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.
|