excel - 遍历工作表/EXCEL VBA 中的所有(即使分组)形状

标签 excel vba iteration shapes

下面的代码不考虑分组的形状。有变通办法吗?

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/

相关文章:

excel - 使用 .bat 文件和任务调度程序来执行 .vbs 文件

Excel VBA,使用评估(MAX(IF))条件的自定义搜索 - 不匹配错误

java - 使用迭代算法进行本地文件搜索

javascript - Excel like 列标题选项过滤算法

excel - 如何使用动态数组函数从具有对角值的列创建对角矩阵

c# - VSTO 2012 : backward compatibility

vba - Excel VBA - 粘贴到标题下方

excel - 为 Excel VBA 解决方案创建用户帮助文档的最佳方法是什么?

python - 第 60 行,在 make_tuple 中返回 tuple(l) TypeError : iter() returned non-iterator of type 'Vector'

python - 以特定方式组合数组元素并记录它