Gadolin
Gadolin

Reputation: 2686

vba collection of shapes

I want to store buttons in some sort of collection, arraylist,
so that I can add and remove dynamically.

I tried to use Collection but seems it is not the choice, as I got an error when ar.Add() is reached.

Object doesn't support this property or method.

 Public Sub removeAllFormsWithAdd()
 Dim myshape As Shape
Dim ar As Collection
For Each myshape In ActiveSheet.Shapes
    If (myshape.FormControlType = xlButtonControl) Then 
    If (myshape.TextFrame.Characters.Text = "name") Then
        ar.Add (myshape)
        Debug.Print "next shape:" & myshape.TextFrame.Characters.Text & "-"
    End If
    End If
    Next myshape
End Sub

How can I get it?

Upvotes: 3

Views: 2984

Answers (1)

GSerg
GSerg

Reputation: 78175

ar.Add() is not reached as ar is Nothing. You should initialize it to a New Collection.

Other than that, remove the parentheses:

ar.Add myshape

With the parentheses, you are trying to add to the collection the value of the default property of the shape object, and Shape doesn't have a default property.

Upvotes: 3

Related Questions