Consulting

Results 1 to 10 of 10

Thread: Sort shapes left to right by selection order (2)

  1. #1

    Sort shapes left to right by selection order (2)

    Hello everyone,


    I'm trying to get a selection of shapes in order from right to left. I found a routine by John Wilson on the board on which I based my code.


    The sorting works perfectly when I select Item by item by clicking on the shapes but doesn't respect the "visible order" of shapes if I select them by "lassoing" with my mouse.


    Please help me fix this.


    Thanks in advance
    Sub AlignFlush()
    Dim oshpR As ShapeRange
    Dim oshp As Shape
    Dim L As Long
    Dim rayPOS() As Single
    Set oshpR = ActiveWindow.Selection.ShapeRange
    ReDim rayPOS(1 To oshpR.Count)
    'add to array
    For L = 1 To oshpR.Count
        rayPOS(L) = oshpR(L).Left
    Next L
    'sort
    Call sortray(rayPOS)
    'apply
    For L = 1 To oshpR.Count
        If L = 1 Then
            Set oshp = Windows(1).Selection.ShapeRange(1)
            PosTop = oshp.Top
            PosNext = oshp.Left + oshp.Width
        Else
            Set oshp = Windows(1).Selection.ShapeRange(L)
            oshp.Top = PosTop
            oshp.Left = PosNext
            PosNext = oshp.Left + oshp.Width
        End If
    Next L
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 04:03 PM. Reason: Reduced the whitespace

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Where is the sortRay(rayPos) routine?
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Sorry, here it is.

    Sub sortray(ArrayIn As Variant)
       Dim b_Cont As Boolean
       Dim lngCount As Long
       Dim vSwap As Long
       Do
          b_Cont = False
          For lngCount = LBound(ArrayIn) To UBound(ArrayIn) - 1
             If ArrayIn(lngCount) > ArrayIn(lngCount + 1) Then
                vSwap = ArrayIn(lngCount)
                ArrayIn(lngCount) = ArrayIn(lngCount + 1)
                ArrayIn(lngCount + 1) = vSwap
                b_Cont = True
             End If
          Next lngCount
       Loop Until Not b_Cont
    End Sub
    Last edited by Aussiebear; 02-05-2022 at 01:45 PM. Reason: Added code tags to the supplied code

  4. #4
    Anyone?

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Not sure what you mean it works no matter how you select (that's the point of it)
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    What I mean is that when I select the shapes by sliding over them with the mouse (or lassoing them with the mouse) the shapes aren't sorted from left to right reflecting order you select them by.

  7. #7
    I mean that if you select the shapes by sliding your mouse over them, the sorting seems to be based on the z-order or some other criteria and not the way the shapes are displayed onscreen left to right. If you select the shapes one by one and sort it respects the order you select whereas when you slide over them and intuitively think that the selection is left to right, the select order is not left to right.

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Just realized the code you have is different to mine here.

    Try

    Sub AlignFlush()
    Dim oshpR As ShapeRange
    Dim oshp As Shape
    Dim L As Long
    Dim rayPOS() As Shape
     Dim PosTop As Long
     Dim PosNext As Long
    Set oshpR = ActiveWindow.Selection.ShapeRange
    
    
    ReDim rayPOS(1 To oshpR.Count)
    'add to array
    For L = 1 To oshpR.Count
      Set rayPOS(L) = oshpR(L)
    Next L
    'sort
    Call sortray(rayPOS)
    'apply
    For L = 1 To UBound(rayPOS)
        If L = 1 Then
     Set oshp = rayPOS(L)
            PosTop = oshp.Top
            PosNext = oshp.Left + oshp.Width
        Else
            Set oshp = rayPOS(L)
            oshp.Top = PosTop
            oshp.Left = PosNext
            PosNext = oshp.Left + oshp.Width
        End If
    Next L
    End Sub
    Sub sortray(ArrayIn As Variant)
       Dim b_Cont As Boolean
       Dim lngCount As Long
       Dim vSwap As Shape
       Do
          b_Cont = False
          For lngCount = LBound(ArrayIn) To UBound(ArrayIn) - 1
             If ArrayIn(lngCount).Left > ArrayIn(lngCount + 1).Left Then
               Set vSwap = ArrayIn(lngCount)
               Set ArrayIn(lngCount) = ArrayIn(lngCount + 1)
               Set ArrayIn(lngCount + 1) = vSwap
                b_Cont = True
             End If
          Next lngCount
       Loop Until Not b_Cont
    End Sub
    Last edited by John Wilson; 02-13-2022 at 01:52 PM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    Perfect! Thanks John

  10. #10
    @johnwilson

    I had no problem adapting the code to work vertically. The original aligns left, my modification aligns from top.

    Now I was trying to get it to align right but the shapes end up all over the place. Then the idea was to adapt flush to bottom.

    Thanks in advance

    Thanks in advance

    Global Const txDrawAreaLeft As Integer = 60
    Global Const txDrawAreaWidth As Integer = 840
    
    Sub AlignRight()
    Dim oshpR As ShapeRange
    Dim oshp As Shape
    Dim L As Long
    Dim rayPOS() As Shape
    Dim PosTop As Long
    Dim PosNext As Long
    Set oshpR = ActiveWindow.Selection.ShapeRange
    ReDim rayPOS(1 To oshpR.Count)
    'add to array
    For L = 1 To oshpR.Count
        Set rayPOS(L) = oshpR(L)
    Next L
    'sort
    Call sortray(rayPOS)
    'apply
    For L = 1 To UBound(rayPOS)
        If L = 1 Then
            Set oshp = rayPOS(L)
            PosTop = oshp.Top
            PosNext = (txDrawAreaLeft + txDrawAreaWidth) - oshp.Width
        Else
            Set oshp = rayPOS(L)
            oshp.Top = PosTop
            oshp.Left = PosNext
            PosNext = (txDrawAreaLeft + txDrawAreaWidth) - oshp.Width
        End If
    Next L
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 04:04 PM. Reason: Edited code layout

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •