下面的代码不考虑分组的形状。有变通办法吗?
Sub LoopThruShapes()
Dim sh As Shape
i=1
For Each sh In ActiveSheet.Shapes
Cells(i, 1).value = sh.name
Next
End Sub
来源: http://www.java2s.com/Code/VBA-Excel-Access-Word/Excel/LoopingthroughaCollectionofShapes.htm
最佳答案
你可以试试这段代码:
Sub test()
EnumShapes ActiveSheet.shapes
End Sub
Function EnumShapes(shps As Shapes)
Dim shp As Shape
Dim subshp As Shape
For Each shp In shps
Debug.Print shp.Name
If shp.Type = msoGroup Then
For Each subshp In shp.GroupItems
Debug.Print Space(2) + subshp.Name
Next subshp
End If
Next shp
End Function
如果分组的形状本身就是一个组,则上面的代码不会识别子组,因为 Excel 会展平 GroupItems 集合中的形状,但它会枚举所有形状(无论它们的深度如何)。
你会得到这样的输出:
Rectangle 1
Group 4
Rectangle 2
Rectangle 3
Group 12
Rectangle 6
Rectangle 7
Rectangle 9
Rectangle 10
关于excel - 遍历工作表/EXCEL VBA 中的所有(即使分组)形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314958/