sorting listbox in excel vba

 In this tutorial I will show only How to sort data in ListBox without changing worksheet.

First of all you need to populate data in listbox. If you want to learn how to display data in listbox then click on this below link or goto https://nsutradhar.blogspot.com/ Display Search results in ListBox with more than 10 Column ( 17 Column ) in excel VBA https://youtu.be/i1k7Vz9Q-iU Populate ListBox based on Textbox Keywords || Display search results in listbox https://youtu.be/zaPpkWYnAN4 ListBox with multiple criteria in excel vba faster method https://youtu.be/TxsjjaWWKFQ How to create account ledger in excel || Multiple criteria filtering in Listbox https://youtu.be/MRNcYoFtYBg
vba codes are below...



Private Sub CommandButton1_Click()
' date sorting
Dim i As Long
Dim j As Long
Dim c As Integer
Dim k As Variant
With ListBox1
 For i = 1 To .ListCount - 1
 For j = i + 1 To .ListCount - 1
 If DateValue(.List(i, 0)) > DateValue(.List(j, 0)) Then
 For c = 0 To 2
 k = .List(i, c)
 .List(i, c) = .List(j, c)
 .List(j, c) = k
 Next c
 End If
 Next j
 Next i
 End With
End Sub

Private Sub CommandButton2_Click()
' alphabeticaaly sorting
Dim i As Long
Dim j As Long
Dim c As Integer
Dim k As Variant
With ListBox1
 For i = 1 To .ListCount - 1
 For j = i + 1 To .ListCount - 1
 If .List(i, 1) > .List(j, 1) Then
 For c = 0 To 2
 k = .List(i, c)
 .List(i, c) = .List(j, c)
 .List(j, c) = k
 Next c
 End If
 Next j
 Next i
 
 End With
End Sub

Private Sub CommandButton3_Click()
' as per number value
Dim i As Long
Dim j As Long
Dim c As Integer
Dim k As Variant
With ListBox1
 For i = 1 To .ListCount - 1
 For j = i + 1 To .ListCount - 1
 If Val(.List(i, 2)) > Val(.List(j, 2)) Then
 For c = 0 To 2
 k = .List(i, c)
 .List(i, c) = .List(j, c)
 .List(j, c) = k
 Next c
 End If
 Next j
 Next i
 
 End With

End Sub

If you want to download this file click below link

Download File

No comments:

Post a Comment