View Full Version : Name and Delete Shape

03-07-2012, 04:06 PM

I have used the following code to create and name a shape :

ActiveSheet.Shapes.AddShape(msoShapeRectangle, 736.25, 330#, 97.25, 63#).Select
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 65
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
With Selection
.Name = ButtonHide
End With

I now want to use a new macro to delete this same shape - however I'm not sure how to reference the named shape, and my knowledge of VBA is limited to copying and trying to understand code I read n the next. Can anybody provide any hints?


03-08-2012, 12:45 AM
Hi there,

I do not do much (if anything) with shapes, but this seems to work for both creating and killing the shape. Please note the Me keyword, as I just did the example in the worksheet's module. Presuming you are writing more code, and have it in a Standard Module, you will want to properly qualify teh sheet.

Sub CreateIt()
Dim shp As Shape

Set shp = Me.Shapes.AddShape(msoShapeRectangle, 736.25, 330#, 97.25, 63#)
With shp

.Fill.Visible = msoTrue
.Fill.ForeColor.SchemeColor = 65
.Fill.Transparency = 0#
.Line.Weight = 0.75
.Line.DashStyle = msoLineSolid
.Line.Style = msoLineSingle
.Line.Transparency = 0#
.Line.Visible = msoTrue

.Name = "ButtonHide"
End With
End Sub

Sub KillIt()
Dim shp As Shape

For Each shp In Me.Shapes
If shp.Name = "ButtonHide" Then
End If

End Sub

Hope that helps,


Bob Phillips
03-08-2012, 01:44 AM
Why loop the shapes to delete it when you know its name?


03-08-2012, 03:38 AM

Why loop the shapes to delete it when you know its name?

Hi Brother:beerchug: ,

I sure hope the weather is not treacherous and that all is blessed with you and yours. Afraid my new(ish) assignment has me keeping all too busy. I miss our occassional 'chats'.

You are right of course; I could have used:

On Error Resume Next

On Error GoTo 0

...to run or run past if non-existant. The loop seemed okay, presuming (or maybe hoping) there's not shapes as far as the eye can see...