Share via

Grouping shapes using VBA

Anonymous
2021-08-14T19:20:12+00:00

I am trying to group 13 textboxes and 1 chart together using Excel VBA, but I get the following runtime error: "The index into the specified collection is out of bounds." Here is my code:

Sub Macro3()

Dim N As Integer 

Dim I As Integer 

Dim A As String 

Dim C(1 To 14) As Variant 

N = 0 

For I = 1 To ActiveSheet.Shapes.Count 

    A = ActiveSheet.Shapes.Item(I).Name 

    If InStr(1, A, "TextBox") Or InStr(1, A, "Chart") Then 

        N = N + 1 

        C(N) = A 

    End If 

Next I 

'ReDim Preserve C(1 To N) 

ActiveSheet.Shapes.Range(Array(C)).Select         ' This is the line where I get the error.

Selection.ShapeRange.Group.Select 

End Sub


Jim Benet

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2021-08-14T21:16:21+00:00

C is already an array, so you shouldn't use Array(C):

ActiveSheet.Shapes.Range(C).Select
Selection.ShapeRange.Group.Select

or

ActiveSheet.Shapes.Range(C).Group

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-08-15T00:51:31+00:00

    Thanks. That worked!

    Was this answer helpful?

    0 comments No comments