VBA编程时,需要什么数据类型一般先声明相应类型的变量。这样,有助于理清编程思路,防止出现数据类型不兼容、丢失精度、浪费存储空间等问题。声明变量类型后,变量所能赋得的值也就相应的收到了限制,具体如下所述:
注:考虑到翻译起来费事费力而且怕翻译的不准确误导别人,所以保留英文原文,以备需要时查阅。
声明类型 |
取值范围 |
Boolean, Byte, Currency, Date, Double, Integer, Long, |
Only data values whose value type has the same name as the declared type. |
Variant |
No restrictions, generally any data value with any value type. However, in some contexts Variant declared types are explicitly limited to a subset of possible data values and value types. |
String*n, where n is an integer between 1 and 65,535 |
Only data values whose value type is String and whose character length is exactly n. |
Fixed-size array whose declared element type is one of Boolean, Byte, Currency, Date, Double, Integer, Long, LongLong, Object, Single, String, String*n, a specific class name, or the name of a UDT. |
Only homogeneous array data values that conform to the following restrictions: |
Fixed-size array whose declared element type is |
Only data values whose value type is Array and that conform to the following restrictions: |
Resizable array whose declared element type is one Boolean, Byte, Currency, Date, Double, Integer, Long, |
Only homogeneous array data values where the value type of every element data value is the same as the variable’s declared element type. If the variable’s element declared type is a specific class name then every element of the data value must be either the object reference Nothing or a data value whose value type is object reference and which identifies either an object that is an instance of the named element class or an object that conforms to the public interface of the named class. |
Resizable array whose declared element type is Variant |
Only data values whose value type is Array. |
Specific class name |
Only the object reference data value Nothing and those data values whose value type is object reference and which identify either an object that is an instance of the named class or an object that conforms to the public interface of the named class. |
Specific UDT name |
Only data values whose value type is the specific named UDT. |