sort combobox

iggydarsa

Well-known Member
Joined
Jun 28, 2005
Messages
1,780
Office Version
  1. 2019
Platform
  1. Windows
Hello...

My excel file sits in a folder with bunch of other files. What I want to do is, populate the combobox in the userform with this file names in alphabetical order. How do i do that?

Thanks all...
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Thanks Datsmart

I guess I will use
Code:
Private Sub UserForm_Initialize() 
    Dim Sh As Worksheet 
    Dim Rng As Range 
    Dim Cell As Range 
    Dim Coll As New Collection 
    Dim Item As Variant 
    Dim i As Long 
    Dim j As Long 
    Dim Temp As Variant 
    Set Sh = Worksheets("Sheet1") 
    Set Rng = Sh.Range("A1:A" & Sh.Range("A" & Sh.Rows.Count).End(xlUp).Row) 
    ComboBox1.Clear 
    On Error Resume Next 
    For Each Cell In Rng 
        Coll.Add Cell.Value, CStr(Cell.Value) 
    Next Cell 
    On Error GoTo 0 
    For Each Item In Coll 
        ComboBox1.AddItem Item 
    Next Item 
    With ComboBox1 
        For i = 0 To .ListCount - 1 
            For j = i + 1 To .ListCount - 1 
                If .List(i) > .List(j) Then 
                    Temp = .List(j) 
                    .List(j) = .List(i) 
                    .List(i) = Temp 
                End If 
            Next j 
        Next i 
    End With 
End Sub

but I have to fill Coll variable with the names of files... do you know how to gather the names of the files (in the same folder with excel file) and put them in the collection? What I am trying to do is, after I select the file name from the combobox I will click on a button and it will open the file...


thanks.
 
Upvote 0
Hi Datsmart,

Your code works great. I changed it a little for my needs (see below). When user click Confirm Button, I need to refer back to the Workbooks("misdfcustomer.xls").Worksheets("custinfo") and extract data that corresponds to the selection made in the combobox. But since the order in the combobox has been re-arranged, how can i correctly reference to the Worksheet?

Private Sub UserForm_Initialize()

Dim k As Long
Dim j As Long
Dim Temp As Variant

OpenNotShow ("misdfcustomer.xls")

ComboBoxCustComp.Clear

'Extract list from worksheet and populate combobox
TotalRow = Workbooks("misdfcustomer.xls").Worksheets("custinfo").Range("A65536").End(xlUp).Row
For i = 2 To TotalRow
ComboBoxCustComp.AddItem Workbooks("misdfcustomer.xls").Worksheets("custinfo").Cells(i, 3).Value
Next i

With ComboBoxCustComp
For k = 0 To .ListCount - 1
For j = k + 1 To .ListCount - 1
If .List(k) > .List(j) Then
Temp = .List(j)
.List(j) = .List(k)
.List(k) = Temp
End If
Next j
Next k
End With

'Use drop-down list
ComboBoxCustComp.Style = fmStyleDropDownList
'Combo box values are ListIndex values
ComboBoxCustComp.BoundColumn = 0
'Set combo box to first entry
ComboBoxCustComp.ListIndex = 0

Workbooks("misdfcustomer.xls").Close SaveChanges:=False

End Sub




Private Sub CBConfirm_Click()

'Extract Data Corresponding To Combo Selection

OpenNotShow ("misdfcustomer.xls")

Workbooks(datafile).Worksheets(refsheet).Cells(NewRow, 3).Value = Workbooks("misdfcustomer.xls").Worksheets("custinfo").Cells(ComboBoxCustComp.Value + 2, 3).Value

Workbooks("misdfcustomer.xls").Close SaveChanges:=False

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

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