Sort and single out ListBox entries

oggy3000

Board Regular
Joined
Jul 13, 2011
Messages
51
Hello,
I have a Combobox which is indirectly controlling the values of a ListBox. Unfortunately the ListBox contains all possible entries. Many of them exist multiple times and they are not sorted.
Is there a possibility to at least get rid of the multiple entries?
If the List could then be organized ascending, it would be perfect :)
Following comes my code for the ListBox

Cheers
Jan
Code:
Private Sub cboPc_Change()
Dim ljh As String
Set wksData = ThisWorkbook.Sheets("Data")
 
    lstLjh.Clear
 
    For i = 2 To wksData.Cells(65536, 5).End(xlUp).Row
        ljh = wksData.Cells(i, 5).Value
        If UserForm1.cboPc.Value = ljh Then
            lstLjh.AddItem wksData.Cells(i, 2)
        End If
    Next i
    ThisWorkbook.Sheets("Filtering").Cells(2, 5).Value = cboPc.Value
 
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You can create "very hidden" sheet, delete there duplicates and sort remained data.
For example:
Code:
[COLOR="Blue"]Sub[/COLOR] SortAndDelete()
    [COLOR="Blue"]With[/COLOR] Sheets("Very Hidden Sheet").Range("A1").CurrentRegion
        .RemoveDuplicates
        .Sort key1:=Sheets("Very Hidden Sheet").Cells(1, 1)
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Thank you for the code. I am not experienced in VBA so I have some problems with it. I understand the idea behind it but how can I get it updated everytime the Value of the combobox is changed? And where to get the Values from?

I found some code of a UserForm that does what I want to do. But I can not find anything that describes the removing of the duplicates (it does not sort the entries). Altering my version to it does not exactly work, so I just need help to alter it to my version.
Code:
Option Explicit
 
Private Sub cbb1_Change()
    Dim hshB As Object
    Dim i As Long
 
    Set hshB = CreateObject("Scripting.Dictionary")
    Me.cbb2.Clear
    Me.cbb3.Clear
 
    With ThisWorkbook.Sheets("Tabelle1")
        For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            If .Cells(i, 1) = Me.cbb1 Then
                hshB(.Cells(i, 2).Text) = 0
            End If
        Next
        Me.cbb2.List = hshB.Keys
    End With
 
    Set hshB = Nothing
End Sub
 
Private Sub cbb2_Change()
    Dim hshC As Object
    Dim i As Long
 
    Set hshC = CreateObject("Scripting.Dictionary")
    Me.cbb3.Clear
 
    With ThisWorkbook.Sheets("Tabelle1")
        For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            If .Cells(i, 2) = Me.cbb2 Then
                hshC(.Cells(i, 3).Text) = 0
            End If
        Next
        Me.cbb3.List = hshC.Keys
    End With
 
    Set hshC = Nothing
End Sub
 
Private Sub cmdOk_Click()
    With Me
        MsgBox .cbb1 & "-" & .cbb2 & "-" & .cbb3
    End With
End Sub
 
Private Sub UserForm_Initialize()
    Dim hshA As Object
    Dim i As Long
 
    Set hshA = CreateObject("Scripting.Dictionary")
 
    With ThisWorkbook.Sheets("Tabelle1")
        For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            hshA(.Cells(i, 1).Text) = 0
        Next
        Me.cbb1.List = hshA.Keys
    End With
 
    Set hshA = Nothing
End Sub
 
Upvote 0
Write the sheet and range which you wanna sort and delete duplicates.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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