Sorted Values ComboBox VBA

evanmer

New Member
Joined
Apr 20, 2012
Messages
14
I'm trying to pull all unique values from a specific column (starting in D2) from worksheet "RawData" and push it to a combobox ("ComboBoxManager")and have gotten it thus far (see code below) but i'd also like to have it sorted from A to Z but am having trouble, can anyone help?

Code:
Private Sub UserForm_Initialize()
Application.ScreenUpdating = False
Dim LastRow&, cell As Range
With Worksheets("RawData")
On Error Resume Next
.ShowAllData
Err.Clear
LastRow = .Cells(Rows.Count, 2).End(xlUp).Row
.Range("D1:D" & LastRow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
ComboBoxManager.Clear
For Each cell In .Range("D2:D" & LastRow).SpecialCells(12)
ComboBoxManager.AddItem cell.Value
Next cell
On Error Resume Next
.ShowAllData
Err.Clear
End With
Application.ScreenUpdating = True
        
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi,

Try this

Code:
Private Sub UserForm_Initialize()
Dim ComBoList      As Variant
    Dim ComBoTemp       As Variant
    Dim x           As Long
    Dim j           As Long
Application.ScreenUpdating = False
Dim LastRow&, cell As Range
With Worksheets("RawData")
On Error Resume Next
.ShowAllData
Err.Clear
LastRow = .Cells(Rows.Count, 2).End(xlUp).Row
.Range("D1:D" & LastRow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
ComboBoxManager.Clear
For Each cell In .Range("D2:D" & LastRow).SpecialCells(12)
ComboBoxManager.AddItem cell.Value
Next cell
On Error Resume Next
.ShowAllData
Err.Clear
End With


    
    ComBoList = Me.ComboBoxManager.List
    
    For x = LBound(ComBoList) To UBound(ComBoList) - 1
        For j = x + 1 To UBound(ComBoList)
            If ComBoList(x, 0) > ComBoList(j, 0) Then
                ComBoTemp = ComBoList(x, 0)
                ComBoList(x, 0) = ComBoList(j, 0)
                ComBoList(j, 0) = ComBoTemp
            End If
        Next j
    Next x
    
    Me.ComboBoxManager.List = ComBoList
Application.ScreenUpdating = True
        
End Sub
 
Last edited:
Upvote 0
Thanks for the help Mike.

I tried the added code and what it seems to do is set the combobox default value to the the first value in my data set after its sorted, however still does not sort the combobox.
 
Upvote 0

Forum statistics

Threads
1,216,069
Messages
6,128,602
Members
449,460
Latest member
jgharbawi

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