XL97: Select Method May Select More Cells Than ExpectedLast reviewed: February 27, 1998Article ID: Q157412 |
The information in this article applies to:
SYMPTOMSWhen you run a Visual Basic for Applications macro that uses the Select method to select a specific range of cells in Microsoft Excel 97, the macro may select a larger range of cells than you specified.
CAUSEThis 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/or 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:
ActiveSheet.Range("A1:A10").SelectThe 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 in Microsoft Excel 97, you cannot select only cells A1:A10 with the mouse. Microsoft Excel automatically extends the selection to include cells B1:C10 because the range A2:C2 is merged.
WORKAROUNDMicrosoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400. To work around this behavior, use one of the following methods.
Method 1Apply the property or method to specific a 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 selection:
Sub FormatCells() ActiveSheet.Range("A1:A10").Select Selection.Font.Bold = True End SubIf cells A2:C2 are merged, this macro applies a bold font format to the cells A1:C10. If you wish to limit the application of the bold font format to cells A1:A10, use the following macro instead. The following example applies a bold font format to the specific Range object:
Sub FormatCells() ActiveSheet.Range("A1:A10").Font.Bold = True End Sub Method 2Another 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:
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 STATUSThis behavior is by design in Microsoft Excel 97.
|
Additional query words: XL97 8.00 8.0 selected selecting code
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |