Qbik
Qbik

Reputation: 6147

Is it possible to create two dimensional array with Array()?

Creation of one dimensional array is straightforward :

Dim A As Variant
A = Array(10,20,30)

Is it possible to create two dimensional array with Array() ? Is there any other method ?

Upvotes: 1

Views: 737

Answers (3)

gembird
gembird

Reputation: 14053

I think it is not possible. With Array function You can only specify a list of values, not dimemsions.

Array Function Syntax

Array(arglist)

The required arglist argument is a comma-delimited list of values that are assigned to the elements of the array contained within the Variant.

And you can affect lower bound. Use Option Base 1 then the Array() function returns array whith lower bound 1. Only if Array() function is qualified with type library name e.g. VBA.Array() then Option Base does not affect the lower bound of the array returned by Array() function.

To create 2D array simply specify dimensions e.g. like this

Dim multiA(1 To 2, 1 To 2) As String

Or use ReDim like this

Dim multiA As Variant
ReDim multiA(1 To 2, 1 To 2)

Upvotes: 1

Tom Robinson
Tom Robinson

Reputation: 1920

You can create an array of arrays using the Array() function syntax:

  x = Array(Array(0, 1, 2, 3, 4), Array(100, 101), Array(200, 201, 202))
  debug.print x(2)(1)
  201

This not actually a two-dimension array. This technique is used a lot in other languages.

Upvotes: 2

Doug Glancy
Doug Glancy

Reputation: 27478

I knew I'd seen this somewhere before, but had to search for a bit:

Sub test()
Dim A As Variant
A = [{1,2,3;4,5,6;7,8,9}]
Debug.Print A(2, 2)
End Sub

Upvotes: 6

Related Questions