VBA Create Array (1 Viewer)

Status
Not open for further replies.

ajetrumpet

Banned
Local time
Today, 03:40
Joined
Jun 22, 2007
Messages
5,638
Static / Fixed Arrays

These are manually dimensionalized. Dimensions and number of elements are defined manually when arrays are declared. Short example:

Code:
    Dim myarray(6) As Variant

These have to be populated from their declaration specs. For example:

Code:
dim i as long

   for i = 0 to 5
      myarray(i) = i
   next i

Dynamic Arrays

These are unspecified in terms of dimensions and number of elements. Example:

Code:
    Dim myarray() As Variant

These have to be redimensionalized everytime an element or a dimension is added. The ReDim statement is used for this. Using ReDim alone will empty the area everytime it is redimensionalized so you have to populate it afterwards. Use this on arrays with more than one dimension. Example:

Code:
dim i as long
dim j as long

   for i = 0 to 5
      for j = 0 to 10
         ReDim(i, j)
      next j
   next i

Using ReDim Preserve saves the values that are already in the array and also redimensionalizes it. It saves time for one-dimensionals, like:

Code:
dim i as long

   for i = 0 to 5
      ReDim Preserve myarray(i)
      myarray(i) = i
   next i

More than one dimension usually does not work with Preserve because the last dimension must not be manipulated when using the key word.
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom