The following limitations apply to variables in the Visual Basic language.
The data segment (that is, the data defined in the Declarations section) of the VBA module of any form or module in Visual Basic can be up to 64K. This data segment contains the following data:
If a procedure or module exceeds the 64K code limit, Visual Basic generates a compile-time error.
If you define a procedure that has more than 64K of local variables defined, you get the error "Too many local nonstatic variables."
If you define a module that has more than 64K of module-level variables defined, or if you define a User-Defined Type larger than 64K, you get the error "Fixed or static data can't be larger than 64K."
If you encounter this error, you can avoid it by breaking extremely large procedures into several smaller procedures, or by moving module-level declarations into another module.
An array declared as a variable doesn't contribute to the entire size of the array; only the array descriptor counts toward the 64K limit. So it is acceptable, for example, to have a declaration such as Dim x(1000000) As Byte
either in a procedure or at module level. Out of memory problems occur, however, if you declare a large, fixed-size array in a record, then declare instances of those records as variables.
No variable of a user-defined type can exceed 64K, although the sum of variable-length strings in a user-defined type may exceed 64K (variable-length strings occupy only 4 bytes each in the user-defined type; the actual contents of a string are stored separately). User-defined types can be defined in terms of other user-defined types, but the total size of the types cannot exceed 64K.
Arguments and local variables in procedures take up stack space at run time. Module-level and static variables do not take up stack space because they are allocated in the data segment for forms or modules. Any DLL procedures you call use this stack while they are executing.
Visual Basic itself uses some of the stack for its own purposes, such as storing intermediate values when evaluating expressions.
Total available stack size for Visual Basic is one megabyte (1MB) per thread. A stack may grow beyond this, however, if there is adjacent free memory.
For More Information For tips on conserving stack space, see "Designing for Performance and Compatibility."