Sort by date of ListBox Contents

IoanZ

Board Regular
Joined
Mar 2, 2018
Messages
51
Hello to everyone.
I found this code wich help to sort by the items from listbox.

So I have a userform with Listbox and a button
The listbox is populated with dates.
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "17.03.2021"
.AddItem "15.03.2021"
.AddItem "13.03.2021"
.AddItem "18.03.2021"
.AddItem "14.03.2021"
.AddItem "16.03.2021"
End sub

After I run this code the items are rearranged cronological:
13.03.2021
14.03.2021
15.03.2021
16.03.2021
17.03.2021
18.03.2021

Private Sub CommandButton1_Click()
Dim i As Long
Dim j As Long
Dim Temp As Variant
With ListBox2
For i = 0 To .ListCount - 2
For j = i + 1 To .ListCount - 1
If DateValue(List(i, 0)) > DateValue(.List(j, 0)) Then
Temp = .List(j, 0)
.List(j, 0) = .List(i, 0)
.List(i, 0) = Temp
End If
Next j
Next i
End With
End Sub

Question: Wich is vba code to create reverse sort of the dates ?

Something like that:
18.03.2021
17.03.2021
16.03.2021
15.03.2021
14.03.2021
13.03.2021

Thank's a lot.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,214,573
Messages
6,120,310
Members
448,955
Latest member
Dreamz high

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