I would like to select shapes with specified conditions on a specified page number, select them into an array at once, and group them into groups, but the system displays an error message for "ActiveSheet.Shapes.Range(MyArray).Select"
Sub group_all()
Dim X, x_count, x_start, x_end As Integer
Dim S As Shape
Dim MyArray
Dim n As Integer
With ActiveSheet
X = 2
If X = 1 Then
x_start = 1
Else:
x_start = .HPageBreaks(X - 1).Location.Row 'x start row
End If
x_end = .HPageBreaks(X).Location.Row - 1 'X end row
'------------------------------------------------------------------------------------------------------
MyArray = Array("")
For Each S In .Shapes
If S.Type = msoGroup Then S.Ungroup
Next S
n = 0
For Each S In .Shapes
If (S.Top > Cells(x_start, 10).Top And S.Top < Cells(x_end, 10).Top + Cells(x_end, 10).Height) And InStr(S.Name, "CommandButton") = 0 Then
MyArray = Split(Join(MyArray, ",") & IIf(n = 0, "", ",") & S.Name, ",")
n = n + 1
End If
Next S
End With
'Array("TextBox 4173", "TextBox 4164", "TextBox 4165", "TextBox 4174")
ActiveSheet.Shapes.Range(MyArray).Select
Selection.ShapeRange.Group.Select
End Sub
How can I fix it, thank you.
Shapes.Range
accept array as parameter, but it's index must start with 1.
Therefore insert this into your code after the End With
statement.
ReDim c(1 To UBound(MyArray) + 1)
For i = 1 To UBound(MyArray) + 1
c(i) = MyArray(i - 1)
Next i
and change parameter
ActiveSheet.Shapes.Range(c).Select