Row and Column Range Converted to Two-Dimensional Array (115513)



The information in this article applies to:

  • Microsoft Excel 97 for Windows

This article was previously published under Q115513

SUMMARY

Page 127 of the Microsoft Excel 5.0 "Visual Basic User's Guide," states that a Microsoft Excel horizontal or vertical array is converted to a one- dimensional Visual Basic array. This information is not entirely correct. When you define a variable in a procedure based on a row or a column of cells on a worksheet, the variable is converted to a two- dimensional array.

Note that the examples used on page 127 of the Microsoft Excel 5.0 "Visual Basic User's Guide" ARE correct. A horizontal or vertical array such as {"Apples",7,19} or {"Apples";7;19} is converted to a one- dimensional Visual Basic array.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. In Microsoft Excel, if you dimension a row or column of cells on a worksheet as an array in a Visual Basic module, and you return a value in the array as an element of a one-dimensional array, you receive the following error message:
Run-time error '9':

Subscript out of range
To avoid receiving the error message above when you return a value from a variable that has been defined as a worksheet row or column, return the value as an element of a two-dimensional array as in the following example.

Sample Visual Basic Macro

   Sub ArrayExample()
     ' Dimension variable Column_Variable as Variant type
     Dim Column_Variable as Variant
     ' Assign variable Column_Variable to named range Vert
     ' Vert contains the vertical range A1:A3
     Column_Variable = ThisWorkbook.Sheets("Sheet1").Range("Vert")
     ' Return value in A2, element in second row, first column in array
     MsgBox Column_Variable(2, 1)
   End Sub
				

REFERENCES

For more information about Creating Arrays, click the Search button in the Visual Basic Reference help and type:

arrays


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbProgramming KB115513