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

Last reviewed: September 2, 1997
Article ID: Q115513
The information in this article applies to:
  • Microsoft Excel for Windows, version 5.0
  • Microsoft Excel for Windows 96, version 7.0
  • Microsoft Excel 97 for Windows

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

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.

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

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 : kbother kbprg PgmOthr xlvbahowto xlvbainfo kbcode kbdocerr kbprg
Version : 5.00 5.00c 7.00 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.