Microsoft KB Archive/125811

= Microsoft Knowledge Base =

XL: Range Method Array Limited to 3267 Elements
Last reviewed: September 13, 1996

Article ID: Q125811

The information in this article applies to:


 * Microsoft Excel for Windows 95, versions 7.0, 7.0a
 * Microsoft Excel for Windows, versions 5.0, 5.0c
 * Microsoft Excel for the Macintosh, versions 5.0, 5.0a
 * Microsoft Excel for Windows NT, version 5.0

SUMMARY
If the Range method in a macro is declared as an array that contains more than 3,276 elements, you may receive one of the following error messages when you attempt to run the macro:

"Range method of Application class failed" - or -

"Unable to get the Value property of the Range class".

WORKAROUND
To declare an array containing more than 3,267 elements, use a loop to declare the array one element at a time. The following Visual Basic for Applications code declares an array containing more than 3,267 elements.

Sample Visual Basic Code
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.

The following subroutine assumes that you have a workbook that contains at least one sheet and that there is data in cells A1 through A3268.

Sub DimensionLargeArray Dim MyArray As Variant Worksheets(1).Activate 'activates the first worksheet in the workbook Range("A1:" & Range("A1").End(xlDown).Address).Select 'selects all the cells in the first column from 'cell "a1" through the first blank cell

'NOTE: If the desired column is other than "A", change the "A1" in both 'Range statements to match the desired column.

ReDim MyArray(Selection.Count) 'counts the number of cells in the range and dimensions the array.

'The For Each loop uses the current cell row number to address the array 'element and to update the row count displayed in the Excel Status Bar. For Each x In Selection

MyArray(Mid(x.Rows.Address, 4)) = x.Value Application.StatusBar = Mid(x.Rows.Address, 4) 'NOTE: If The column number is greater than 26 (column Z), then use a 5 in 'place of the 4 in both "Mid(x.Rows.Address, 4)" locations. Next End Sub