XL: Row and Column Range Converted to Two-Dimensional Array

ID: Q115513

The information in this article applies to:

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

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

Additional query words: 1.00 5.00 7.00 8.00 97 err msg docerr
Keywords          : kbprg kbdta KbVBA 
Version           : WINDOWS:5.0,5.0c,7.0,97
Platform          : WINDOWS

Last Reviewed: May 17, 1999