Consulting

Results 1 to 4 of 4

Thread: Name and Delete Shape

  1. #1
    VBAX Newbie
    Joined
    Oct 2011
    Posts
    5
    Location

    Name and Delete Shape

    Hi,

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

    [VBA]ActiveSheet.Shapes.AddShape(msoShapeRectangle, 736.25, 330#, 97.25, 63#).Select
    Selection.ShapeRange.Fill.Visible = msoTrue
    Selection.ShapeRange.Fill.Solid
    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[/VBA]

    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?

    Thanks
    Last edited by Bob Phillips; 03-08-2012 at 01:41 AM. Reason: Added VBA tags

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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.

    [VBA]Sub CreateIt()
    Dim shp As Shape

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

    .Fill.Visible = msoTrue
    .Fill.Solid
    .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
    shp.Delete
    End If
    Next

    End Sub[/VBA]

    Hope that helps,

    Mark

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why loop the shapes to delete it when you know its name?

    [vba]
    ActiveSheet.Shapes("ButtonHide").Delete
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @xld:
    Quote Originally Posted by xld
    Why loop the shapes to delete it when you know its name?
    Hi Brother ,

    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:
    [vba]
    On Error Resume Next
    WhateverReference.Shapes("ButtonHide").Delete
    On Error GoTo 0
    [/vba]
    ...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...

    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •