XL: Finding Graphic Objects Within a Range with TopLeftCell

ID: Q125179


The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition


SUMMARY

Graphic objects, such as charts or text box objects, are drawn on the object layer of a worksheet. The object layer is above the worksheet cells. In Microsoft Visual Basic for Applications, you can determine the object's cell coordinates by using the TopLeftCell property.


MORE INFORMATION

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/overview/overview.asp
The following Visual Basic macro (Sub procedure) determines whether a graphic object is within the print range. If it is, the object is deleted. The TopLeftCell property returns the cell that is under the top left portion of the graphic object.

The items on a worksheet that would be considered part of the graphic objects collection include:
Arc, Button, ChartObject, CheckBox, Drawing, DropDown, GroupBox, GroupObject, Label, Line, ListBox, OLEObject, OptionButton, Oval, Picture, Rectangle, ScrollBar, Spinner, and TextBox.

Sample Visual Basic Procedure


   Sub DeleteObjects()
       ' Define a For-Each loop of all the drawing objects on the
       ' active worksheet.
       For Each DrawObj In ActiveSheet.DrawingObjects
           ' Define Cell_Ref as the top left cell address of each Drawing
           ' Object.
           Cell_Ref = DrawObj.TopLeftCell.Address
           ' Define a For-Each loop of all the cells with the range
           ' Print_Area.
           For Each Cell In Range("Print_Area")
               ' If the Object's top left cell address is the same as a
               ' cell in the print range.
               If Cell_Ref = Cell.Address Then
                   ' Then delete the object.
                   DrawObj.Delete
               ' End the IF statement.
               End If
           ' Loop through next cell.
           Next Cell
       ' Loop through next DrawObj.
       Next DrawObj
   End Sub 

Additional query words: 5.00c XL97 XL98 XL7 XL5

Keywords : kbprg kbdta kbdtacode PgmOthr PgmHowto KbVBA
Version : MACINTOSH:5.0,5.0a,98; WINDOWS:5.0,5.0c,7.0,7.0a,97; winnt:5.0
Platform : MACINTOSH WINDOWS winnt
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.