Programmatically add shapes to a selection

E

Edward Ulle

I have a worksheet with various shapes already on it. I add new shapes.
I want to collect the new shapes only and group them.

How do I build a selection consisting of the newly created shapes.

The number of added shapes is dynamic so I don't know of any way to use
Array(...) with Range and the shape indexes.

TIA
 
T

Tom Ogilvy

This adds a variable number of rectangles and selects them:

Sub Macro2()
ActiveCell.Select
num = Int(Rnd() * 5 + 1)
Debug.Print num
For i = 1 To num
l = Rnd() * ActiveWindow.VisibleRange.Width
t = Rnd() * ActiveWindow.VisibleRange.Height
Debug.Print t, l
ActiveSheet.Shapes.AddShape(msoShapeRectangle, l, t, _
55.5, 18#).Select False
Next
End Sub

The false argument to select says to add to the currently selected shape(s)
 
E

Edward Ulle

Tom,

Thanks.

The select method is so far down the chain I couldn't find it.

By the way do you ever sleep?

Ed
 
D

Dan Fisher

Edward,

I recently had a similar challenge and this is how I was able to do it...

When I created the new shapes, I added some identifying characters to each
shape name. (.Name = "XXX" & .Name). Then when I wanted to find the
shapes, I iterated through all of the shapes in the worksheet. When I found
one whose name began with the string that I added, I added that name to an
array of variants. It must be an array of Variants, not strings. Then I
created a ShapeRange set containing the names that I wanted (Set SR =
ActiveSheet.Shapes.Range(ShapeNames()), where ShapeNames() is the array of
names) Then you could execute SR.Select to get where you want.

In my case I created a Shape Group. I was able to get it to work in this
manner. Hope that this helps.

Dan Fisher
 

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