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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

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,130,169
Messages
5,640,550
Members
417,151
Latest member
ChickenTenderer

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