I have 6 shapes in a grid of 3x2. With their 'shape number' in the textbox as their respective z-order:
I currently have a code from another thread that rearranges the z-order of shapes from top to bottom, left to right:
Function orderShapes(shapeRange) As shape()
'Adds selected shapes into an array from top to bottom, left to right
If shapeRange.Count = 0 Then Exit Function
' Fill an array with all shapes.
ReDim shapeArray(1 To shapeRange.Count) As shape
Dim i As Long, j As Long
For i = 1 To shapeRange.Count
Set shapeArray(i) = shapeRange(i)
Next
' Sort by position (left/top).
For i = 1 To UBound(shapeArray) - 1
For j = i To UBound(shapeArray)
Dim left1 As Double, left2 As Double, top1 As Double, top2 As Double
left1 = Round(shapeArray(i).Left, 1)
left2 = Round(shapeArray(j).Left, 1)
top1 = Round(shapeArray(i).Top, 1)
top2 = Round(shapeArray(j).Top, 1)
If top1 > top2 Or (top1 = top2 And left1 > left2) Then
Dim tmpShape1 As shape
Set tmpShape = shapeArray(i)
Set shapeArray(i) = shapeArray(j)
Set shapeArray(j) = tmpShape
End If
Next
Next
orderShapes = shapeArray
End Function
Sub setZOrderOfShapes()
' Rearranges the z-oarder of the shape array
Dim a() As shape, i As Long
a = orderShapes(ActiveWindow.Selection.shapeRange)
For i = 1 To UBound(a)
a(i).ZOrder msoBringToFront
Next
End Sub
The code above works as intended in two scenarios; shapes are already aligned top for each row (as the 'top1 = top2 And left1 > left2' condition will execute to determine the order) or if each shape in each row has a higher x-position to the shape to the right, like:
Is there a way to introduce a way to align the first row's shapes to top using .align msoAlignTops, and then doing the same for the second row of shapes and so on such that they look like:
I am unbale to get the function to loop through the first row (first three shapes), and then the next row (next three shapes) etc.