Mamoud
Mamoud

Reputation: 57

Access a Shape inside another one in VBA

I have big shapes on my worksheet (a group of shapes), and inside everyone of them others little shapes (the blue rectangles),

enter image description here

I made a for loop for inside each Big shape to fill automatically the little shapes, but how can I loop over the Big ones, because all the big shapes are similars, and they have the same names for the littles shapes inside ?

How can I acces the little shape from the big one ?

I tried this but didn't worked

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"

The "gr" variable takes every time the name of the big shapes (Graph_1 .. Graph_5) and the x variable takes the name of the little shapes inside (rect_1 .. rect_21)

I think this code does not help because my approach it's like how to access a case in an array if I can say ?

For Each myshape In ActiveSheet.Shapes

Upvotes: 1

Views: 2464

Answers (1)

cyboashu
cyboashu

Reputation: 10433

You can access child shapes inside a group by using following example:

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

Here... you should have figured it out yourself :)

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

Upvotes: 1

Related Questions