Sorting list boxes in VBA

Sorting list boxes in VBA

Anonymous
Not applicable
872 Views
3 Replies
Message 1 of 4

Sorting list boxes in VBA

Anonymous
Not applicable
Does anyone have some code to sort list boxes in Autocad's VBA?

Thanks for your help
0 Likes
873 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
I use this for combo boxes, maybe you can adapt it to suit your needs:

calling procedure:
Dim vSort() As Variant
'sort combo box
vSort = Sort_Combo(cComboBox.List)
cComboBox.List = vSort


'Assumptions:
'Combo box passed in array of variants (0 to #, 0 to 9), with
'vArray(#,0) as the item to sort by
'
Public Function Sort_Combo(vArray As Variant) As Variant

Dim u As Long, l As Long, I As Long
Dim vTemp() As Variant
Dim vA As Variant, vB As Variant
Dim bSort As Boolean

bSort = False
vTemp = vArray
u = UBound(vArray, 1): l = LBound(vArray, 1)

Do
bSort = False
For I = l To u - 1
If vTemp(I, 0) <= vTemp(I + 1, 0) Then
'Debug.Print vTemp(i, 0) & " vs " & vTemp(i + 1, 0)
'this situation is ok
Else
'need to swap
bSort = True
vA = vTemp(I, 0)
vB = vTemp(I + 1, 0)
vTemp(I, 0) = vB
vTemp(I + 1, 0) = vA
'Debug.Print "chg'd " & vTemp(i, 0) & " w/ " & vTemp(i + 1,
0)
End If
Next
Loop Until bSort = False

Sort_Combo = vTemp

End Function

' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . .


--
Kevin


"William Jimenez" wrote in message
news:f13bcd3.-1@WebX.maYIadrTaRb...
> Does anyone have some code to sort list boxes in Autocad's VBA?
>
> Thanks for your help
>
>
0 Likes
Message 3 of 4

Anonymous
Not applicable
I have drawings with thousands of layers and had to wait a long time to get
a combobox to sort using a vba bubble sort. I thought ADO might be able to
do it faster so I wrote this little ditty that sorts a collection. It could
probably be improved upon...

Public Function AdoSortComboBox(Combo_Box As ComboBox, Collection_Name As
Variant)

Dim ctr As Integer
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
.Fields.Append "FIELD1", adVarChar, 80, adFldUpdatable
.Open
For ctr = 0 To Collection_Name.Count - 1
.AddNew
!Field1 = Collection_Name(ctr).name
.MoveNext
Next ctr
.Sort = "FIELD1"
.MoveFirst
Do Until rs.EOF
Combo_Box.AddItem rs!Field1
.MoveNext
Loop
.Close
End With
Set rs = Nothing

End Function

Also, make sure you have a reference to ADO.

Regards,
Rogue

"William Jimenez" wrote in message
news:f13bcd3.-1@WebX.maYIadrTaRb...
> Does anyone have some code to sort list boxes in Autocad's VBA?
>
> Thanks for your help
>
>
0 Likes
Message 4 of 4

Anonymous
Not applicable
Although this might be overkill for your project, you could use a different control and let *it* do the sorting for you.



If you right-click on your toolbox and choose Additional Controls you can select Microsoft ListView Control which has a Sorted property. All you have to do is add the items to the control and by the time you've finished they're already sorted!



Regards



Wayne Ivory

IT Analyst Programmer

Wespine Industries Pty Ltd
0 Likes