XL: Error Using Array with Range Method

Last reviewed: February 3, 1998
Article ID: Q149240

The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

SYMPTOMS

In Microsoft Excel, you can store cell addresses in a user-defined array. If you attempt to select the entire array of cells using the Range method in Microsoft Visual Basic for Applications code, you may receive one of the following error messages:

   Run-time error '1004':
   Range method of Application class failed

   -or-

   Run-time error '1004':
   Method 'Range' of object '_Global' failed

CAUSE

The way in which cell addresses are stored in an array may cause the Range method to fail when you select the entire array of cells. To illustrate this, the following array named myArray loads an array of cell addresses and allows the Range method to select the entire range:

   Dim myArray
   myArray = Selection.Address
   Range(myArray).Select

Using this statement stores the selected range to the myArray array in the format of:

   myArray("A1, A3, A5, A7")

However, the following Visual Basic for Applications method stores the selected cells to the array and selecting the array results in the macro error stated above:

   Option Base 1

   Sub ArrayExample()
       Dim myArray()
       For counter = 1 To 10
           ReDim Preserve myArray(counter)
           myArray(counter) = Cells(counter, 1)
       Next counter
       Range(myArray).Select
   End Sub

Using this looping procedure stores the selected range to the array myArray in the format of:

   myArray("A1", "A3", "A5", "A7")

This format causes the Range method to fail when selecting the entire array of addresses.

WORKAROUND

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 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 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/default.asp

Use a range Object instead of an array to allow the entire array to be selected as in the following example:

   Dim dataRange
   Set dataRange = Nothing
   For Each x In Selection
       If dataRange Is Nothing Then
           Set dataRange = x
       Else
           Set dataRange = Union(dataRange, x)
       End If
   Next x
   dataRange.Select

REFERENCES

For more information about Arrays or Ranges in Microsoft Excel 5.0, click the Search button in Help and type:

   Array

   -or-

   Range object

REFERENCES

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications


Additional query words: 5.00 7.00 8.00 XL98 XL97 XL7 XL5 non-contiguous
discontiguous
Keywords : kbcode kberrmsg kbprg PgmOthr
Version : WINDOWS:5.x,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbprb
Solution Type : kbworkaround


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: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.