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: Select Method May Select More Cells Than Expected


View products that this article applies to.

This article was previously published under Q213579

↑ Back to the top


Symptoms

When you run a Visual Basic for Applications macro that uses the Select method to select a specific range of cells, the macro may select a larger range of cells than you specified.

↑ Back to the top


Cause

This problem may occur when you run a macro that uses the Select method to select a range of cells and performs an action on the selected cells.

If you programmatically select a merged cell in a range, the selection may be enlarged such that it encompasses the columns and rows that are occupied by the merged cell. For example, if the range A2:C2 is merged, the following statement selects cells A1:C10 and not A1:A10 as you might expect:
Sub FormatCells()

   ActiveSheet.Range("A1:A10").Select
   Selection.Font.Bold = True

End Sub
				
The selection is expanded to include B1:C10 because range A1:A10 contains a merged cell that extends into cells in columns B and C.

NOTE: In this scenario, you cannot select only cells A1:A10 with the mouse. Excel automatically extends the selection to include cells B1:C10 because the range A2:C2 is merged.

↑ Back to the top


Workaround

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. To work around this behavior, use either of the following methods.

Method 1

Apply the property or method to a specific Range object rather than the Selection object if the selection contains merged cells that span cells outside of that specified range.

For example, the following macro selects cells A1:A10 and applies a bold font format to the specific Range object:
Sub FormatCells()
   ActiveSheet.Range("A1:A10").Font.Bold = True
End Sub
				

Method 2

Another alternative is to check whether merged cells exist in the range prior to performing an action. The following macro determines if there are any merged cells in the range A1:A10 prior to formatting the cells. If the selection contains merged cells, it does not apply the bold formatting:
Sub FormatCells()
    ActiveSheet.Range("A1:A10").Select
     'Apply Bold to the selection if it does not contain merged cells.

    If Not(Selection.MergeCells = True) Then
        Selection.Font.Bold = True
    End If
End Sub
				

↑ Back to the top


Keywords: KB213579, kbprogramming, kbprb

↑ Back to the top

Article Info
Article ID : 213579
Revision : 6
Created on : 10/10/2006
Published on : 10/10/2006
Exists online : False
Views : 254