简体   繁体   English

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

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

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.运行时错误 438 Object 不支持此方法或属性。

I am basing this code on VBA examples off the web - I am not a strong VBA programmer.我将此代码基于 web 的 VBA 示例 - 我不是一个强大的 VBA 程序员。 What is the right way to do this?这样做的正确方法是什么? I am working with excel 2007 and switching excel versions isn't an option.我正在使用 excel 2007 和切换 excel 版本不是一个选项。

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

No need to select first, just use无需先 select,直接使用即可

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

PS. 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.您收到错误的原因是选择 object 指向工作表上选择的任何内容(不会是刚刚创建的形状)很可能是RangeRange没有Shapes属性。 If you happened to have other shapes on the sheet and they were selected then they would be grouped.如果您碰巧在工作表上有其他形状并且它们被选中,那么它们将被分组。

This worked for me in Excel 2010:这在 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.我在表格 1 上有两个形状,在调用上述方法之前未分组,之后分组。

Edit编辑

To select specific shapes using indexes:到 select 使用索引的特定形状:

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

Using names:使用名称:

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

Here's how you can easily group ALL shapes on a worksheet that doesn't require you to Select anything:以下是您可以轻松地在不需要您Select的工作表上对所有形状进行分组的方法:

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:如果您认为/知道当前工作表上的任何形状已经分组,那么您需要首先Ungroup对这些形状的分组:

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我知道这个答案有点题外话,但已经添加了它,因为对 Excel 形状分组的所有搜索都倾向于指向这个问题

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.我有同样的问题,但需要 select 几个形状(以前由宏创建并列在形状数组中),但不是“select.all”,因为幻灯片上可能有其他形状。

The creation of a shaperange was not really easy.创建 shaperange 并不容易。 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".最简单的方法是循环遍历形状对象(如果您已经知道它们)和 select 它们模拟“按住 ctrl 键”选项“替换:= 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.希望有帮助,克里。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel VBA 2007中为表设置变量范围? - How can I set a variable range to a table in excel VBA 2007? 如何在Excel 2007中关闭VBA中的自动更正? - How do I turn off autocorrect in VBA in Excel 2007? 如何使用VBA Excel 2007确定文件是否存在? - How Do I Determine If File Exists Using VBA Excel 2007? 如何以编程方式冻结Excel 2007 VBA中Excel工作表的顶行? - How can I programmatically freeze the top row of an Excel worksheet in Excel 2007 VBA? 为什么我无法在VBA Excel中将这两个形状组合在一起? - Why am i not able to group these two shapes in vba excel? 如何以编程方式双击VBA / Excel中的单元格? - How do I programmatically double-click a cell in VBA/Excel? 如何在Excel 2007中以编程方式更改图表中系列的线条颜色 - How do I programmatically change the line colour for a series in a chart in Excel 2007 在 VBA 中的 Access 2007 中编辑后如何保存 Excel 电子表格 - how do i save a excel spreadsheet after editing it in access 2007 in VBA 如何使用vba在Excel 2007中找到有条件格式化单元格的填充颜色值? - How do I find the fill colour value of a conditionally formatted cell in Excel 2007 using vba? Excel 2007 VBA宏逐行读取文本文件如何停止逗号(,)的分隔 - Excel 2007 VBA Macro reading a text file line by line how do I stop delimiting on a comma (,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM