Delete buttons on sheet

User avatar
ErikJan
BronzeLounger
Posts: 1253
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Delete buttons on sheet

Post by ErikJan »

I use this (and I believe it worked fine before):

Code: Select all

        Dim btn As Shape
         For Each btn In ActiveSheet.Shapes
            If btn.Type = 8 Then btn.Delete
        Next
All looks fine, in the loop I get hits for buttons when btn.type=8 and even btn.name is correct, still I get an error on the btn.delete...
2013-10-22 21_42_42-Microsoft Visual Basic.png
Anyone who has an idea how to avoid this...?
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete buttons on sheet

Post by HansV »

When deleting items from a collection, it's best to loop backwards:

Code: Select all

    Dim i As Long
    For i = ActiveSheet.Shapes.Count To 1 Step -1
        If ActiveSheet.Shapes(i).Type = 8 Then
            ActiveSheet.Shapes(i).Delete
        End If
    Next i
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1253
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Delete buttons on sheet

Post by ErikJan »

I just found and tried exactly that... same problem...

User avatar
ErikJan
BronzeLounger
Posts: 1253
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Delete buttons on sheet

Post by ErikJan »

Update: it already works and I now understand why it worked before... In this code update my worksheet was protected... Once I removed that, all was fine again. I apologize...

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Delete buttons on sheet

Post by rory »

If you just want to delete Forms buttons:

Code: Select all

Activesheet.Buttons.Delete
should be enough.
Regards,
Rory