I try to write a VBA code that will distirbute verticaly selected shapes but based on shapes centres, not spaces between shapes (as default in powerpoint distribute vertaly function). I have started to writing a code but it does not work well from logical perspective in all usecases
I would be glad to help me find error and how to fix that
Sub DistributeShapesVerticallyv2()
Dim oShp As Shape
Dim oSld As Slide
Dim oSel As Selection
Dim i As Integer
Dim shapeCount As Integer
Dim minY As Single
Dim maxY As Single
Dim spaceBetween As Single
' Get the current slide and selection
Set oSld = ActiveWindow.View.Slide
Set oSel = ActiveWindow.Selection
' Check if there are at least two shapes selected
If oSel.Type <> ppSelectionShapes Or oSel.ShapeRange.Count < 2 Then
MsgBox "Please select at least two shapes to distribute vertically."
Exit Sub
End If
' Sort the shapes by their Y values
For i = 1 To oSel.ShapeRange.Count - 1
For j = i + 1 To oSel.ShapeRange.Count
If oSel.ShapeRange(i).Top + oSel.ShapeRange(i).Height / 2 > oSel.ShapeRange(j).Top + oSel.ShapeRange(j).Height / 2 Then
oSel.ShapeRange(i).ZOrder msoSendBack
End If
Next j
Next i
' Initialize variables
minY = oSel.ShapeRange(1).Top + oSel.ShapeRange(1).Height / 2
shapeCount = oSel.ShapeRange.Count
maxY = oSel.ShapeRange(shapeCount).Top + oSel.ShapeRange(shapeCount).Height / 2
' Calculate the space between the shapes
spaceBetween = (maxY - minY) / (oSel.ShapeRange.Count - 1)
' Distribute the shapes vertically
For i = 2 To oSel.ShapeRange.Count
oSel.ShapeRange(i).Top = oSel.ShapeRange(i - 1).Top + oSel.ShapeRange(i - 1).Height / 2 + spaceBetween - oSel.ShapeRange(i).Height / 2
Next i
End Sub
I tried use ChatGPT to find problem but without a success