MS-Excel / General Formatting

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

DefTypeData Type
DefBoolBoolean
DefByteByte
DefIntInteger
DefLngLong
DefCurCurrency
DefSngSingle
DefDblDouble
DefDateDate
DefStrString
DefObjObject
DefVarVariant
[Previous] [Contents] [Next]