We start with some Excel 2007 VBA to draw a shape of several parts, and to group those parts. Somewhat condensed for legibility, the code so far looks like:
So far, so good.Set sh1 = .shapes.AddPolyline(...) Set sh2 = .shapes.AddShape(Type:=msoShapeRectangle, Left:=...) sh1.Name = "Part1" sh2.Name = "Part2" sh1.Fill.ForeColor.RGB = ... sh1.Line.Visible = msoFalse ...
Then
That works fine (even if I can't explain why it has a lower-case "s" on "shapes"). But then I run the code again and Excel reportsshapes.Range(Array("Part1", "Part2")).Group
> Run-time error '1004':
> Grouping is disabled for the selected shapes.
My guess is that Excel 2007 is attempting to group the first-made items with names "Part1" and "Part2", which are already part of a group. Hence the failure.
Please, is it possible to specify the Shapes.Range(Array(...)) using the shape pointers (sh1 and sh2) rather than the names?
Thank you.
Last edited by jdaw1_SG; 11-03-2011 at 04:05 AM. Reason: shapes, range, array, name
You're probably right, can't name multiple shapes the same thing. Do you need to? Maybe:
With ActiveSheet Set sh1 = .Shapes.AddShape(msoShapeRectangle, 0, 18.75, 96.75, 30.75) Set sh2 = .Shapes.AddShape(msoShapeRectangle, 100, 18.75, 96.75, 30.75) .Shapes.Range(Array(sh1.Name, sh2.Name)).Group.Select End With
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thank you, but I had tried that, unsuccessfully.
Is there a way to get an object's index/position number within its parent?
I just tried it and it worked. What is failing for you?
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Does it work for you if you run it a second time?
Yes, of course. My version creates another grouped set on top of the first, but move them out of the way and you'll see the new ones. This is an example, by NOT naming them, the macro will continue to work because it is attaching the sh1 and sh2 variable to the randomly named shapes as they are created, so it should work every time.
You just need to work out the rest of the undisclosed parameters... my macro shows a full example, yours did not.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thank you. That shows the solution.
suffix = Format(Now, "YYYYMMDDHHMMSS") & "_" & Mid("" & Rnd(), 3) sh1.Name = "Part1_" & suffix ... Set shG = .shapes.Range(Array(sh1.Name, sh2.Name, sh3.Name)).Group
You're completely ignoring my suggestion as a whole. I set the sh1 and sh2 variables at the same moment the variables were created, so VBA already knows what they're called. So there is no need to "name" them at all, which appears to be what you are still struggling with.
Anyway, my approach works, but if you've got your own, that's good, too.
If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Marked solved, I hope. Thank you.
My preference is that names be useful to humans. So that, if taking something apart much later, it is clear what is what. For that reason I don't like machine-generated random names, even if uniqueness is guaranteed. So I'm naming, manually, as a sensible names suffixed withI agree that it would be neater to do that at the instant of creation, rather than a few lines later. Presumably for a “.AddShape(Type:=msoShapeRectangle, ...)”, I could add a “Name=”. Can something similar be done for a “.AddPolyline(...)”?suffix = Format(Now, "YYYYMMDDhhmmss") & "_" & Mid("" & Rnd(), 3)
Yes, since it returns a Shape object too.
So “.AddPolyline(...).Name="Piece" & suffix ”?
That’s easy: thank you again.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks