Article ID: 142586
Article Last Modified on 10/11/2006
Sub CenterObjectOverRange()
'Set specified Range.
Set MyRange = Range("C3:D4")
'Add an object, specify the width and height you want the
'object to be in points.
Set MyObject = ActiveSheet.Rectangles.Add(0, 0, 20, 10)
'Set top and left based on the selected range and
'object size to center it.
MyObject.Left = ((MyRange.Width / 2) + MyRange.Left) - _
(MyObject.Width / 2)
MyObject.Top = ((MyRange.Height / 2) + MyRange.Top) - _
(MyObject.Height / 2)
End Sub
The following sample macro adds a rectangle and sizes it to the outline of
the range C3:D4.
Sub AddObjectOnRange()
'Set current Range.
Set MyRange = Range("C3:D4")
'Add object with Top, Left, Width, and Height based on MyRange.
ActiveSheet.Rectangles.Add MyRange.Left, MyRange.Top, _
MyRange.Width, MyRange.Height
End Sub
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: 5.00a 5.00c 8.00 98 XL97 XL7 XL5
Keywords: kbdtacode kbhowto KB142586