How do I group a set of shapes programmatically in excel 2007 vba?

亡梦爱人 提交于 2019-12-04 06:39:55

This worked for me in Excel 2010:

Sub GroupShapes()

    Sheet1.Shapes.SelectAll
    Selection.Group

End Sub

I had two shapes on sheet 1 which were ungrouped before calling the method above, and grouped after.

Edit

To select specific shapes using indexes:

Sheet1.Shapes.Range(Array(1, 2, 3)).Select

Using names:

Sheet1.Shapes.Range(Array("Oval 1", "Oval 2", "Oval 3")).Select

No need to select first, just use

Set shpGroup = shapeSheet.Shapes.Range(Array(Box1.Name, Box2.Name)).Group

PS. the reason you get the error is that the selection object points to whatever is selected on the sheet (which will not be the shapes just created) most likely a Range and Range does not have a Shapes property. If you happened to have other shapes on the sheet and they were selected then they would be grouped.

I had the same problem, but needed to select a couple of shapes (previously created by the macro and listed in an array of shapes), but not "select.all" because there might be other shapes on the slide.

The creation of a shaperange was not really easy. The easiest way is just to cycle through the shape objects (if you already know them) and select them simulating the "hold ctrl key down" with the option "Replace:=False".

So here's my code:

For ix = 1 To x
    bShp(ix).Select Replace:=False
Next
ActiveWindow.Selection.ShapeRange.Group

Hope that helps, Kerry.

Here's how you can easily group ALL shapes on a worksheet that doesn't require you to Select anything:

ActiveSheet.DrawingObjects.Group

If you think/know that there are already groupings for any shapes on your current worksheet, then you'll need to first Ungroup those shapes:

ActiveSheet.DrawingObjects.Ungroup  'include if groups already exist on the sheet
ActiveSheet.DrawingObjects.Group

I'm aware this answer is slightly off-topic but have added it as all searches for Excel shape grouping tend to point to this question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!