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.