在VBA中访问另一个中的Shape

时间:2016-08-08 09:40:33

标签: vba excel-vba excel

我的工作表上有很多形状(一组形状),其中每个人都有小形状(蓝色矩形),

enter image description here

我为每个Big形状内部制作了一个for循环,以自动填充小形状,但是我怎样才能遍历大形状,因为所有大的形状都是类似的,并且它们的内部形状相同?

如何从大的形状中获取小形状?

我尝试了这个,但没有工作

ActiveSheet.Shapes.Range(gr).Select
ActiveSheet.Shapes.Range(Array(x)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = Format(Range(y_1).Value, "#,##0") & " k" & Chr(13) & Format(Range(y_2).Value, "#,##0.00") & " DT"

" gr"变量每次都取大形状的名称(Graph_1 .. Graph_5) 并且x变量采用内部小形状的名称(rect_1 .. rect_21)

我认为这段代码没有帮助,因为我的方法就像我可以说如何访问数组中的案例一样?

For Each myshape In ActiveSheet.Shapes

1 个答案:

答案 0 :(得分:1)

您可以使用以下示例访问组内的子形状:

Sub test()

    Dim shp             As Shape
    Dim shpChild        As Shape

    For Each shp In Sheet1.Shapes
        If shp.Type = msoGroup Then
            '/ Found a group. List all the child shapes here.
            For Each shpChild In shp.GroupItems
                Debug.Print "Child name :" & shpChild.Name & " Group name : " & shp.Name
            Next
         Else
            '/ No group. Individual shape.
             Debug.Print shp.Name
        End If
    Next

End Sub

这里......你应该自己弄明白:)

enter image description here

Sub test()

    Dim shp             As Shape
    Dim shpChild        As Shape

    For Each shp In Sheet1.Shapes
        If shp.Type = msoGroup And shp.Name = "A" Then
            '/ Found a group called "A". List all the child shapes here.
            For Each shpChild In shp.GroupItems
                If shpChild.Name = "X" Then
                    '/ Found X.
                    shpChild.TextFrame2.TextRange.Text = "Hello from VBA!!!"
                End If
            Next
         Else
            '/ No group. Individual shape.
             Debug.Print shp.Name
        End If
    Next

End Sub