A ListBox
没有内置排序功能。你将需要推出自己的。
其基本思路是将列表数据放入数组中,对数组进行排序,然后将数据放回列表中。排序VBA数组有很多很好的参考。
除非您有大量的文件,否则简单的排序可能就足够了。试试这个
Sub SortListBox(oLb As MSForms.ListBox)
Dim vItems As Variant
Dim i As Long, j As Long
Dim vTemp As Variant
'Put the items in a variant array
vItems = oLb.List
' Sort
For i = 0 To UBound(vItems, 1) - 1
For j = i + 1 To UBound(vItems, 1)
If vItems(i, 0) > vItems(j, 0) Then
vTemp = vItems(i, 0)
vItems(i, 0) = vItems(j, 0)
vItems(j, 0) = vTemp
End If
Next
Next
'Clear the listbox
oLb.Clear
'Add the sorted array back to the listbox
For i = 0 To UBound(vItems, 1)
oLb.AddItem vItems(i, 0)
Next
End Sub