Hi all,
I'm having problems changing a code to my needs.
I need to sort some listboxes in a Userform (excel 2003).
All is well, but one...
Code used is from the Ozgrid forum: (http://www.ozgrid.com/forum/showthread.php?t=71509)
But how do I sort columns with dates in it?
- Also it doesnt mather how I format the date (yyyy/mm/dd) it always becomes the system standard (dd/mm/yyyy)
Any help is welcome!
Mathijs.
I'm having problems changing a code to my needs.
I need to sort some listboxes in a Userform (excel 2003).
All is well, but one...
Code used is from the Ozgrid forum: (http://www.ozgrid.com/forum/showthread.php?t=71509)
Code:
Sub SortListBox(oLb As MSForms.ListBox, sCol As Integer, sType As Integer, sDir As Integer)
Dim vaItems As Variant
Dim i As Long, j As Long
Dim c As Integer
Dim vTemp As Variant
'Put the items in a variant array
vaItems = oLb.List
'Sort the Array Alphabetically(1)
If sType = 1 Then
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
'Sort Ascending (1)
If sDir = 1 Then
If vaItems(i, sCol) > vaItems(j, sCol) Then
For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
vTemp = vaItems(i, c)
vaItems(i, c) = vaItems(j, c)
vaItems(j, c) = vTemp
Next c
End If
'Sort Descending (2)
ElseIf sDir = 2 Then
If vaItems(i, sCol) < vaItems(j, sCol) Then
For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
vTemp = vaItems(i, c)
vaItems(i, c) = vaItems(j, c)
vaItems(j, c) = vTemp
Next c
End If
End If
Next j
Next i
'Sort the Array Numerically(2)
'(Substitute CInt with another conversion type (CLng, CDec, etc.) depending on type of numbers in the column)
ElseIf sType = 2 Then
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
'Sort Ascending (1)
If sDir = 1 Then
If CInt(vaItems(i, sCol)) > CInt(vaItems(j, sCol)) Then
For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
vTemp = vaItems(i, c)
vaItems(i, c) = vaItems(j, c)
vaItems(j, c) = vTemp
Next c
End If
'Sort Descending (2)
ElseIf sDir = 2 Then
If CInt(vaItems(i, sCol)) < CInt(vaItems(j, sCol)) Then
For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
vTemp = vaItems(i, c)
vaItems(i, c) = vaItems(j, c)
vaItems(j, c) = vTemp
Next c
End If
End If
Next j
Next i
End If
'Set the list to the array
oLb.List = vaItems
End Sub
You would run it by calling the procedure like this:
Run "SortListBox", [ListBox Name],[ListBox column to sort by],[Alpha(1) or Numeric(2) Sort],[Ascending(1) or Descending(2) Order]
But how do I sort columns with dates in it?
- Also it doesnt mather how I format the date (yyyy/mm/dd) it always becomes the system standard (dd/mm/yyyy)
Any help is welcome!
Mathijs.