powerpivotlegal
New Member
- Joined
- May 14, 2014
- Messages
- 30
Hello,
I'm trying to have a SearchUserForm populate a combo box with non-duplicate unique values that is sorted alphabetically when the form is launched.
The following code gives me the unique values in the combo box but the list is not sorted.
Private Sub UserForm_Initialize()
'Empty ProjectNameSearchComboBox
ProjectNameSearchComboBox.Text = ""
'Empty PartnerNameSearchTextBox
PartnerNameSearchTextBox.Text = ""
'Empty StatusSearchTextBox
StatusSearchTextBox.Text = ""
'Empty CommentsSearchTextBox
CommentsSearchTextBox.Text = ""
Dim oneValue As Variant
Set SourceSheet = Worksheets("Project List")
LastRow = SourceSheet.Cells(Rows.Count, 1).End(xlUp).Row
With ThisWorkbook.Sheets("Project List").Range("A2:A" & LastRow)
For Each oneValue In .Value
If (Application.CountIf(.Cells, CStr(oneValue)) = 1) Then
Me.ProjectNameSearchComboBox.AddItem CStr(oneValue)
End If
Next oneValue
End With
End Sub
The other code I've found on another forum post (shown below) gives me the unique values sorted alphabetically but it displays the first alphabetical option in the combo box as a default rather than a blank combo box.
Private Sub UserForm_Initialize()
'Empty ProjectNameSearchComboBox
ProjectNameSearchComboBox.Text = ""
'Empty PartnerNameSearchTextBox
PartnerNameSearchTextBox.Text = ""
'Empty StatusSearchTextBox
StatusSearchTextBox.Text = ""
'Empty CommentsSearchTextBox
CommentsSearchTextBox.Text = ""
Application.ScreenUpdating = False
Dim Coll As Collection, cell As Range, LR As Long
Dim unsorted As Boolean, i As Integer, temp As Variant
Dim SourceSheet As Worksheet
Set SourceSheet = Worksheets("Project List")
LR = SourceSheet.Cells(Rows.Count, 1).End(xlUp).Row
On Error Resume Next
Set Coll = New Collection
With ProjectNameSearchComboBox
.Clear
For Each cell In SourceSheet.Range("A2:A" & LR)
If Len(cell) <> 0 Then
Err.Clear
Coll.Add cell.Text, cell.Text
If Err.Number = 0 Then .AddItem cell.Text
End If
Next cell
unsorted = True
Do
unsorted = False
For i = 0 To UBound(.List) - 1
If .List(i) > .List(i + 1) Then
temp = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = temp
unsorted = True
Exit For
End If
Next i
Loop While unsorted = True
.ListIndex = 0 'optional
End With
Set Coll = Nothing
Set SourceSheet = Nothing
Application.ScreenUpdating = True
End Sub
How can I fix either code to give me an alphabetical sort of just the unique/non-duplicate values in Column A where the combo box does not display default options or the first option in the list?
Thanks.
I'm trying to have a SearchUserForm populate a combo box with non-duplicate unique values that is sorted alphabetically when the form is launched.
The following code gives me the unique values in the combo box but the list is not sorted.
Private Sub UserForm_Initialize()
'Empty ProjectNameSearchComboBox
ProjectNameSearchComboBox.Text = ""
'Empty PartnerNameSearchTextBox
PartnerNameSearchTextBox.Text = ""
'Empty StatusSearchTextBox
StatusSearchTextBox.Text = ""
'Empty CommentsSearchTextBox
CommentsSearchTextBox.Text = ""
Dim oneValue As Variant
Set SourceSheet = Worksheets("Project List")
LastRow = SourceSheet.Cells(Rows.Count, 1).End(xlUp).Row
With ThisWorkbook.Sheets("Project List").Range("A2:A" & LastRow)
For Each oneValue In .Value
If (Application.CountIf(.Cells, CStr(oneValue)) = 1) Then
Me.ProjectNameSearchComboBox.AddItem CStr(oneValue)
End If
Next oneValue
End With
End Sub
The other code I've found on another forum post (shown below) gives me the unique values sorted alphabetically but it displays the first alphabetical option in the combo box as a default rather than a blank combo box.
Private Sub UserForm_Initialize()
'Empty ProjectNameSearchComboBox
ProjectNameSearchComboBox.Text = ""
'Empty PartnerNameSearchTextBox
PartnerNameSearchTextBox.Text = ""
'Empty StatusSearchTextBox
StatusSearchTextBox.Text = ""
'Empty CommentsSearchTextBox
CommentsSearchTextBox.Text = ""
Application.ScreenUpdating = False
Dim Coll As Collection, cell As Range, LR As Long
Dim unsorted As Boolean, i As Integer, temp As Variant
Dim SourceSheet As Worksheet
Set SourceSheet = Worksheets("Project List")
LR = SourceSheet.Cells(Rows.Count, 1).End(xlUp).Row
On Error Resume Next
Set Coll = New Collection
With ProjectNameSearchComboBox
.Clear
For Each cell In SourceSheet.Range("A2:A" & LR)
If Len(cell) <> 0 Then
Err.Clear
Coll.Add cell.Text, cell.Text
If Err.Number = 0 Then .AddItem cell.Text
End If
Next cell
unsorted = True
Do
unsorted = False
For i = 0 To UBound(.List) - 1
If .List(i) > .List(i + 1) Then
temp = .List(i)
.List(i) = .List(i + 1)
.List(i + 1) = temp
unsorted = True
Exit For
End If
Next i
Loop While unsorted = True
.ListIndex = 0 'optional
End With
Set Coll = Nothing
Set SourceSheet = Nothing
Application.ScreenUpdating = True
End Sub
How can I fix either code to give me an alphabetical sort of just the unique/non-duplicate values in Column A where the combo box does not display default options or the first option in the list?
Thanks.