Arrays
Arrays are variables that identify a set of values. That set has one or more dimensions, and each item within the set is identified by an index within that dimension, as illustrated in Figures 2-11 and 2-12.
Figure 2-11. One-dimensional arrays are simply lists
Figure 2-12. Arrays with two dimensions are tables
Arrays can have more than two dimensions, but that starts to get hard to illustrate on paper. One- and two-dimensional arrays are by far the most common. You use arrays whenever you have a set of data that you want to work with as a unit—lists and tables are typical examples of when to use an array.
The items in an array must all have the same data type
, but that’s not very restrictive considering that you can have arrays of Variants
, which can assume any other type. For example, the following array contains the names of products and their prices. To prove that the stored prices are numbers, I calculate the tax on each price before displaying the table in the Immediate window:
Sub ShowVariantArray( ) Const RATE = 0.06 Dim PriceChart(4, 1) As Variant PriceChart(0, 0) = "Grudgeon" PriceChart(0, 1) = 24.95@ PriceChart(1, 0) = "Pintle" PriceChart(1, 1) = 11.15@ PriceChart(2, 0) = "Tiller" PriceChart(2, 1) = 93.75@ PriceChart(3, 0) = "Rudder" PriceChart(3, 1) = 42.49@ Dim i As ...
Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.