Sorting dates in a listbox

Emperor

Board Regular
Joined
Mar 25, 2010
Messages
225
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)

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.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,203,543
Messages
6,056,023
Members
444,840
Latest member
RazzelDazel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top