Sorting ListBox

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,280
Office Version
  1. 2013
Platform
  1. Windows
Hello,
Is it possible to sort the datas from A to Z when the ListBox2 has been populated?
On the ListBox2 first column is populating the names
Many Thanks


VBA Code:
Private Sub CommandButton12_Click()
Application.ScreenUpdating = False

On Error Resume Next

Dim J As Long, Dic As Object, rng As Range, Ac As Integer
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
Set rng = Sheets("overtimelist").Range("A:A").CurrentRegion.Resize(, 6)
ReDim nRay(1 To rng.Count, 1 To 6)
For J = 1 To rng.Count
    If Not Dic.Exists(rng(J, 1).Value) Then
       Dic.Add rng(J, 1).Value, rng(J, 5).Text
    Else
        Dic(rng(J, 1).Value) = Dic(rng(J, 1).Value) + rng(J, 5).Value
    End If
        For Ac = 1 To 6
            nRay(J, Ac) = rng(J, Ac).Text
        Next Ac
Next J
With ListBox1
    .ColumnCount = 6
    .ColumnWidths = "140;100;90;100;70;0"
    .list = nRay
End With
With ListBox2
    .ColumnCount = 2
    .ColumnWidths = "150;80"
    .list = Application.Transpose(Array(Dic.keys, Dic.Items))
 End With

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Add this line as shown
Rich (BB code):
Set Dic = DicSortAscending(Dic)
With ListBox2
And use this function
VBA Code:
Function DicSortAscending(InDic As Object) As Object
   Dim Ky As Variant
   Dim AryLst As Object
   Dim i As Long
   
   Set AryLst = CreateObject("System.Collections.ArrayList")
   If InDic.Count > 1 Then
      With AryLst
         For Each Ky In InDic.Keys
            .Add Ky
         Next Ky
         .Sort
         Set DicSortAscending = CreateObject("Scripting.Dictionary")
         For i = 0 To .Count - 1
            DicSortAscending.Add .Item(i), InDic(.Item(i))
         Next
      End With
   Else
      DicSortAscending = InDic
   End If
End Function
 
Upvote 0
Hello,
Thanks for the reply.
Could you please help me more on that coz I don't know where to add this code exactly.
Thanks again.
 
Upvote 0
Just put the Function in a standard module & add the line in blue to your existing code where shown.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
It does work on Win 10 as that is what I have. You may need to download & install .Net Framework 3.5
 
Upvote 0
Ok I will try,
How can I use that code to sort on second column if there are numbers also from largest to smalest?
Many Thanks
 
Upvote 0
That's a different question, so needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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