+ Reply to Thread
Results 1 to 5 of 5

Deleting Shapes

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2011
    Location
    Belfast
    MS-Off Ver
    Excel 2007
    Posts
    34

    Deleting Shapes

    Hello all!

    I have a macro that will add text boxes to a form template depending on the contents of cell D4:
    If Range("D4").Value = "OGD" Then
    ActiveSheet.OLEObjects.Add(ClassType:="Forms.TextBox.1", Link:=False, _
            DisplayAsIcon:=False, Left:=3.75, Top:=270, Width:=333.75, Height:= _
            62.25).Select
        Selection.Name = "1"
    It goes on to add another 6 text boxes to the template. If D4="FS" then it adds another 8 boxes. I want to be able to delete the boxes using a macro but can't work out how to tell it that if boxes 8 and 9 are there then delete them and if not then carry on. At the moment it gives me an error message if 8 and 9 aren't there.

    I have other shapes in there so I can't do delete all shapes.

    I hope that makes sense!

    Thanks,

    Dean
    Last edited by romperstomper; 11-25-2011 at 11:53 AM. Reason: code tags

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Deleting Shapes

    Hi,

    If it helps I recently wrote the following code to delete all shapes on a sheet - with exceptions as necessary. You may be able to adapt it.

    Sub RemoveSHapes()
        Dim wShape As Object
       
       ' the exceptions are 'drop' = drop down cells like for example validation drop downs
       ' 'labe' = Label
       ' 'comb' = Combo Box
       ' 'opti' = Option Button
        For Each wShape In Sheet2.Shapes
            If LCase(Left(wShape.Name, 4)) <> "drop" And _
               LCase(Left(wShape.Name, 4)) <> "labe" And _
               LCase(Left(wShape.Name, 4)) <> "comb" And _
               LCase(Left(wShape.Name, 4)) <> "opti" Then
                wShape.Delete
            End If
        Next wShape
      
    End Sub
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    09-20-2011
    Location
    Belfast
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Deleting Shapes

    Thanks Richard. Unfortunately I can't get it to work. Could be due to my editing!

    What I want is the macro equivelent of saying to someone "If you find the text box called 8 delete it, if not, no worries"

    The rest of this spreadsheet is working really well and this is leterally the last thing I need to do to it! So frustrating!

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Deleting Shapes

    Hi,

    In that case just
    Sub DeleteTestBox8()
     Dim wShape As Object
       
        For Each wShape In ActiveSheet.Shapes
            If wShape.Name = "TextBox8" Then
                wShape.Delete
            End If
        Next wShape
    End Sub

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Deleting Shapes

    Why so complicated ?

    You can have all these ActiveX-controls in the worksheet, set to visible=false.
    If you need them to be visible dependent on the values in the worksheet you can use .visible=true.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1