ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
MBMatt Bieringer10/10/2017

Hello all,

I currently have a macro that will create a drawing from a part or assembly. It creates a new sheet for each configuration. My issue comes when I try to reorder the sheets via a bubblesort I get some odd results.

The code:

'Sorts Sheets

Dim vSheetArr As Variant
vSheetArr = swDraw.GetSheetNames

For i = 0 To SheetCount - 1
    sheetName = vSheetArr(i)
    If sheetName Like "*OP*" Then
        sheetName = Right(sheetName, Len(sheetName) - 3)
        vSheetArr(i) = CLng(sheetName)
    End If
Next i

Call Sort(vSheetArr)
For i = 0 To SheetCount - 1
    sheetName = vSheetArr(i)
    If IsNumeric(sheetName) Then
        sheetName = "OP " & sheetName
        vSheetArr(i) = sheetName
    End If
Next I

bool = swDraw.ReorderSheets(vSheetArr)
If bool = False Then
    MsgBox "NO WORKIE"
    Unload Me
End If

Sub Sort(vSheetArr As Variant)      'Sort Sheets

Dim first           As Long
Dim last            As Long
Dim i               As Long
Dim j               As Long

Dim Temp            As Long
Dim list            As Long

    first = LBound(vSheetArr)
    last = UBound(vSheetArr)

    For i = first To last - 1
        For j = i + 1 To last
            If vSheetArr(i) > vSheetArr(j) Then
                Temp = vSheetArr(j)
                vSheetArr(j) = vSheetArr(i)
                vSheetArr(i) = Temp
            End If
        Next j
    Next i

End Sub

So if I have configurations OP 1, OP 2, OP 3, OP 10, & OP 100. The sort will output this as: OP 1, OP 10, OP 100, OP 2, & OP 3.

Any help would be appreciated.