Finding Graphic Objects Within a Range with TopLeftCell (125179)



The information in this article applies to:

  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 5.0
  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q125179

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. 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. 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
				

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming KB125179