Skip to content

Arrays

An array holds a series of values in one variable, each reached by its position.

Declaring an array

With a size, giving the highest index it will hold:

Dim Warehouses(5)

Without a size, ready for ReDim to size it later:

Dim Warehouses()

From a list of values, using the Array function:

Dim Warehouses
Warehouses = Array("MAIN", "NORTH", "RETURNS")

By splitting a delimited field. This is where most arrays in an integration come from:

Dim Codes
Codes = Split(%WarehouseList, ",")

Split takes the string and the delimiter, and returns a zero-based array. Join(Codes, ",") puts one back together.

Check what Split gave you before indexing it

Split on a string holding no delimiter returns one element rather than none. Codes(0) is then the whole string. Split on an empty string returns an array of no elements at all, whose UBound is -1, and Codes(0) raises "Subscript out of range". Test UBound before reading a position:

Dim Codes
Dim Second

Codes = Split(%WarehouseList, ",")
Second = ""

If UBound(Codes) >= 1 Then Second = Codes(1)

Second

With %WarehouseList holding MAIN,NORTH, the expression returns NORTH. With MAIN alone, or nothing, it returns an empty string.

An array of 5 holds six values

Indexes start at zero. Dim Warehouses(5) gives six positions, 0 to 5. LBound returns the first index and UBound the last. The size cannot be negative.

An array holds values of different types at once, because every element is a Variant:

Dim Line(2)
Line(0) = "IC-1000"        ' String
Line(1) = 100              ' Number
Line(2) = #10/07/2013#     ' Date, 7 October 2013

Reading and writing an element

The index goes in brackets after the name:

Dim Warehouses
Warehouses = Array("MAIN", "NORTH", "RETURNS")
Warehouses(1)

The expression returns NORTH.

Arrays of more than one dimension

A second number in the declaration gives a second dimension. An array can have up to 60, and two is the common case:

Dim Grid(2, 3)
Grid(0, 0) = "IC-1000"
Grid(2, 3) = "IC-2400"

Dim Grid(2, 3) is three rows by four columns, on the same rule as a single dimension. UBound(Grid, 1) returns 2 and UBound(Grid, 2) returns 3.

Resizing an array

ReDim changes the size of an array that was declared without one.

ReDim alone clears every value:

Dim Warehouses()
ReDim Warehouses(2)
Warehouses(0) = "MAIN"
ReDim Warehouses(4)     ' Warehouses(0) is now Empty

ReDim Preserve keeps the values that still fit:

Dim Warehouses()
ReDim Warehouses(2)
Warehouses(0) = "MAIN"
ReDim Preserve Warehouses(4)     ' Warehouses(0) is still MAIN

Growing an array with Preserve adds the new positions at the end holding Empty. Shrinking one keeps the positions that remain and discards the rest. A value dropped by a shrink is gone, and growing the array again does not bring it back.

Erase empties an array. On an array declared with a size it sets every element back to Empty and keeps the size. On one declared without a size it discards the elements altogether, and UBound then raises "Subscript out of range".

ReDim needs an array declared without a size

ReDim against an array that was given a size in its Dim fails at run time with "This array is fixed or temporarily locked", error 10. Declare it as Dim Warehouses() when the size is not known until the expression runs.