Changing the Default Data Type
The preceding section that VBA assigns the Variant type to a variable if you don't specify a data type. However, VBA supports a number of DefType statements that let you redefine the default data type. These statements all use the following syntax:
DefType letter1[-letter2]
Here, Type is a three or four-letter code that specifies the data type, and letter1 and letter2 define a range of letters. Note that this is a module-level statement, so you must place it at the top of a module, before any procedures or functions.
The idea is that any variable (or function argument or function result) that begins with one of these letters will be assigned the specified data type by default. For example, the DefInt keyword is used to set the default data type to Integer. If you want VBA to assign, say, the Integer data type to any variables that begin with the letters X through Z, you would add the following statement at the module level:
DefInt X-Z
Lists the various DefType keywords and the data types they represent.
VBA's DefType keywords
DefType | Data Type |
DefBool | Boolean |
DefByte | Byte |
DefInt | Integer |
DefLng | Long |
DefCur | Currency |
DefSng | Single |
DefDbl | Double |
DefDate | Date |
DefStr | String |
DefObj | Object |
DefVar | Variant |
In this tutorial:
- Understanding Program Variables
- Declaring Variables
- Avoiding Variable Errors
- Variable Data Types
- Changing the Default Data Type
- Creating User-Defined Data Types
- Using Array Variables
- Dynamic Arrays
- Multidimensional Arrays
- Working with Constants
- Storing User Input in a Variable
- Getting Return Values from the Message Dialog Box
- Getting Input Using InputBox