Range.EntireRow May Return Incorrect Result (108518)



The information in this article applies to:

  • Microsoft Excel for Windows 95 7.0a
  • Microsoft Excel for Windows 5.0c
  • Microsoft Excel for Windows NT 5.0
  • Microsoft Excel for the Macintosh 5.0a

This article was previously published under Q108518

SYMPTOMS

In the versions of Microsoft Excel listed at the beginning of this article, the Range.EntireRow method may return an incorrect result if the Range is a nonadjacent selection. For example, if the current Range is the nonadjacent selection $A$1:$A$3, $C$11:$C$13, the Range.EntireRow method will return $1:$13, not $1:$3,$11:$13 as would be expected. This also occurs when you use the Range.EntireColumn method to return entire columns when the Range is nonadjacent.

WORKAROUND

To return the correct rows or columns in a selection when the selection is nonadjacent, you must separate the selection into Areas, where an Area is defined as a single piece of a nonadjacent selection. This process is illustrated in the Visual Basic code example below.

Visual Basic Code Example

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to 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 requirements. NOTE: To convert this example to work with columns, use "Column" in place of "Row".

To use the SelectRows Sub procedure, select any combination of cells on Sheet1. (This example assumes the current selection is $A$1:$A$3,$C$11:$C$13 on Sheet1.) Then, click Macro on the Tools menu, select the name SelectRows from the list of macro names, and click Run.
     Option Explicit

   Sub SelectRows()

      ' Dimension some variables.
      Dim Counter As Integer, X As Variant, Y As Variant

      Sheets("Sheet1").Activate          ' Ensure the worksheet is active.

      ' Sets range X equal to the current selection.
      Set X = Selection
      ' Initializes range Y equal to the first Area in X.
      Set Y = X.Areas(1)

      ' Iterate through the loop once for each Area (nonadjacent piece)
      ' of the range X.
      For Counter = 1 To X.Areas.Count
         ' Set Y equal to the union of its previous range and the range of
         ' the rows which encompass the current Area.
         Set Y = Application.Union(Y, X.Areas(Counter).EntireRow)
      Next                                ' Loop until done.

      Y.Select                            ' Select the range Y.

   End Sub
				
For example, if you select the range $A$1:$A$3,$C$11:$C$13 on worksheet Sheet1 and then run the SelectRows Sub procedure, the procedure determines how many Areas there are in the selection (there are two: $A$1:$A$3 and $C$11:$C$13), determines which combination of rows encompasses each Area ($1:$3, $11:$13), and selects the range $1:$3,$11:$13.

STATUS

Microsoft has confirmed this to be a problem in the versions of Microsoft Excel listed at the beginning of this article. This problem was corrected in Microsoft Excel 97 for Windows and Microsoft Excel 98 Macintosh Edition.

MORE INFORMATION

You can create a nonadjacent selection by selecting a range and then selecting another range while holding down the CTRL key on your keyboard or by using Microsoft Excel version 4.0 or Visual Basic commands that select two or more ranges at the same time.

Modification Type:MinorLast Reviewed:10/10/2006
Keywords:kbbug kbProgramming KB108518