XL97: Select Method May Select More Cells Than ExpectedID: Q157412
|
When 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.
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/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").Select
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.
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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/To work around this behavior, use one of the following methods.
Sub FormatCells()
ActiveSheet.Range("A1:A10").Select
Selection.Font.Bold = True
End Sub
If 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
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
Additional query words: XL97 8.00 8.0 selected selecting code
Keywords : kbprg kbdta kbdtacode xlui KbVBA xlvbmigrate
Version : WINDOWS:97
Platform : WINDOWS
Issue type :
Last Reviewed: July 2, 1999