如何在 excel 2007 vba 中以编程方式对一组形状进行分组?

发布于 2024-11-14 01:55:36 字数 1254 浏览 2 评论 0原文

我正在迭代电气表表上的数据并在形状表上创建形状。创建形状后,我想以编程方式对它们进行分组。但是我无法找出正确的语法。形状就在那里,被选中,如果我单击分组按钮,它们就会完美分组。但是通过下面的代码我得到

运行时错误 438 对象不支持此方法或属性。

我的这段代码基于网上的 VBA 示例 - 我不是一个强大的 VBA 程序员。这样做的正确方法是什么?我正在使用 Excel 2007,并且无法切换 Excel 版本。

有问题的代码片段:

Set shapeSheet = Worksheets("Shapes")

With shapeSheet
    Selection.ShapeRange.Group.Select
End With

上下文:

Dim shapeSheet As Worksheet
Dim tableSheet As Worksheet
Dim shpGroup As Shape

Set shapeSheet = Worksheets("Shapes")
Set tableSheet = Worksheets("Electrical Tables")


With tableSheet
    For Each oRow In Selection.Rows
            rowCount = rowCount + 1
            Set box1 = shapeSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 50 + ((rowCount - 1) * 14), 115, 14)
            box1.Select (False)
            Set box1Frame = box1.TextFrame
            Set box2 = shapeSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 165, 50 + ((rowCount - 1) * 14), 40, 14)
            box2.Select (False)
            Set box2Frame = box2.TextFrame
     Next
End With

Set shapeSheet = Worksheets("Shapes")

With shapeSheet
    Selection.ShapeRange.Group.Select
End With

I am iterating over data on the Electrical Tables sheet and creating shapes on a Shape sheet. Once the shapes are created I would like to programmatically group them. However I can't figure out the right syntax. The shapes are there, selected, and if I click the group button, they group perfectly. However with the following code I get

Runtime Error 438 Object does not support this method or property.

I am basing this code on VBA examples off the web - I am not a strong VBA programmer. What is the right way to do this? I am working with excel 2007 and switching excel versions isn't an option.

Problematic snippet:

Set shapeSheet = Worksheets("Shapes")

With shapeSheet
    Selection.ShapeRange.Group.Select
End With

Context:

Dim shapeSheet As Worksheet
Dim tableSheet As Worksheet
Dim shpGroup As Shape

Set shapeSheet = Worksheets("Shapes")
Set tableSheet = Worksheets("Electrical Tables")


With tableSheet
    For Each oRow In Selection.Rows
            rowCount = rowCount + 1
            Set box1 = shapeSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 50 + ((rowCount - 1) * 14), 115, 14)
            box1.Select (False)
            Set box1Frame = box1.TextFrame
            Set box2 = shapeSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 165, 50 + ((rowCount - 1) * 14), 40, 14)
            box2.Select (False)
            Set box2Frame = box2.TextFrame
     Next
End With

Set shapeSheet = Worksheets("Shapes")

With shapeSheet
    Selection.ShapeRange.Group.Select
End With

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那伤。 2024-11-21 01:55:36

无需先选择,直接使用

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

PS即可。出现错误的原因是选择对象指向工作表上选择的任何内容(这将不是是刚刚创建的形状),很可能是 RangeRange 没有 Shapes 属性。如果纸张上碰巧有其他形状并且选择了它们,那么它们将被分组。

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.

没有伤那来痛 2024-11-21 01:55:36

这在 Excel 2010 中对我有用:

Sub GroupShapes()

    Sheet1.Shapes.SelectAll
    Selection.Group

End Sub

我在工作表 1 上有两个形状,它们在调用上述方法之前未分组,并在调用上述方法之后分组。

编辑

要使用索引选择特定形状:

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

使用名称:

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

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
离线来电— 2024-11-21 01:55:36

以下是如何轻松地对工作表上的所有形状进行分组,而不需要您选择任何内容:

ActiveSheet.DrawingObjects.Group

如果您认为/知道您的工作表上已经有任何形状的分组当前工作表,那么您需要首先取消分组这些形状:

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

我知道这个答案有点偏离主题,但已添加它,因为所有对 Excel 形状分组的搜索都倾向于指向这个问题

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

兰花执着 2024-11-21 01:55:36

我遇到了同样的问题,但需要选择几个形状(之前由宏创建并在形状数组中列出),但不是“select.all”,因为幻灯片上可能还有其他形状。

创建形状范围并不容易。
最简单的方法就是循环浏览形状对象(如果您已经知道它们)并选择它们,模拟“按住 ctrl 键”并使用选项“Replace:=False”。

这是我的代码:

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

希望有帮助,
凯里.

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.

无所的.畏惧 2024-11-21 01:55:36

我可以在这里看到很多解决方案,但我想与您分享我在不知道形状名称或编号且不使用 ActiveSheetSelect 的情况下处理此主题的方法。

下面的代码将对设置工作表上的每个形状进行分组。

Dim arr_txt() As Variant
Dim ws As Worksheet
Dim i as Long

set ws = ThisWorkbook.Sheets(1)

With ws
    ReDim arr_txt(1 To .Shapes.Count)
    For i = 1 To .Shapes.Count
        arr_txt(i) = i 'or .Shapes(i).Name
    Next
    .Shapes.Range(arr_txt).Group
End With

I can see many solutions here, but I would like to share with You my way to deal with this topic without knowing shapes name or number and without using ActiveSheet or Select.

The code below will group every shape on set worksheet.

Dim arr_txt() As Variant
Dim ws As Worksheet
Dim i as Long

set ws = ThisWorkbook.Sheets(1)

With ws
    ReDim arr_txt(1 To .Shapes.Count)
    For i = 1 To .Shapes.Count
        arr_txt(i) = i 'or .Shapes(i).Name
    Next
    .Shapes.Range(arr_txt).Group
End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文