Select Multiple Shape Objects

G

Guest

How do I select multiple Shape Objects?

I have many shape objects on a worksheet and would like to select a subset
of them based on their name, ie select all Shape Objects whose name begins
with "MyName". I can then group and move them in single operation.

I have the following:

Dim ShapeObjectNameArray() as string

i = 0
for each shapeobject in Worksheet(1).Shapes
if Left(shapeobject.Name, 6) = "MyName" then
ShapeObjectNameArray(i) = shapeobject.Name
i = i + 1
end if
next shapeobject

Redim Preserve ShapeObjectNameArray(i)

Worksheet(1).Shapes.Range(ShapeObjectNameArray).Select

There is a runtime error stating a parameter has an invalid value. Appears
the Range(ShapeObjectNameArray) is unacceptable. The following works but
does not select all the desired shapes:

Worksheet(1).Shapes.Range(Array(ShapeObjectNameArray(1),
ShapeObjectNameArray(2))).Select

I've tried creating arrays and collections of the desired shape object names
and shape objects, then use these as an argument for "Range". They all fail.

Any suggestions?

As a less desirable alternative, I'm willing to select each shape object
individually as i loop thru the shapes. Is there a way to select one object,
then another, then another, etc? Every time I Select an object, the previous
selection is lost. I haven't found an "AddToSelection" or similar method.

thanks.
 
D

Doug Glancy

Alan,

This works for me:

Sub test()

Dim shapeobject As Shape
Dim i As Long
Dim ShapeObjectNameArray()

i = 0
For Each shapeobject In Worksheets(1).Shapes
If Left(shapeobject.Name, 6) = "MyName" Then
ReDim Preserve ShapeObjectNameArray(i)
ShapeObjectNameArray(i) = shapeobject.Name
i = i + 1
End If
Next shapeobject
Worksheets(1).Shapes.Range(ShapeObjectNameArray).Select

End Sub

hth,

Doug Glancy
 
G

Guest

Thanks for the quick response.

After a few experiments it works.

I removed "As String" from my array declaration.

I removed my "Redim" statements before and after the loop and replaced them
with a single "Redim" statement inside the loop.

I'm a little confused why this was causing a problem.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top