Sort by date of ListBox Contents

IoanZ

Board Regular
Joined
Mar 2, 2018
Messages
50
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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
Change '>' to '<'.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,931
Messages
5,639,064
Members
417,067
Latest member
rohitbabshet

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
Top