Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Error When You Use Array with Range Method


View products that this article applies to.

This article was previously published under Q213326

↑ Back to the top


Symptoms

In Microsoft Excel 2000, you can store cell addresses in a user-defined array. If you attempt to select the entire array of cells by 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

↑ Back to the top


Cause

The way that 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
				
By using this statement, you store the selected range to the myArray array in the following format:
myArray("A1, A3, A5, A7")
However, the following Visual Basic for Applications method stores the selected cells to the array. Selecting the array results in one of the macro error message described in the "Symptoms" section.
Option Base 1

Sub ArrayExample()
   Dim myArray()
   For counter = 1 To 1
      ReDim Preserve myArray(counter)
      myArray(counter) = Cells(counter, 1)
   Next counter
   Range(myArray).Select
End Sub
				
By using this looping procedure, you store the selected range to the myArray array in the following format:
myArray("A1", "A3", "A5", "A7")
This format causes the Range method to fail when you select the entire array of addresses.

↑ Back to the top


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 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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: To work around this issue, 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
				

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


References

For more information about arrays, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type arrays in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213326, kbprogramming, kbpending, kberrmsg, kbdtacode, kbbug

↑ Back to the top

Article Info
Article ID : 213326
Revision : 7
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 294