+ Reply to Thread
Results 1 to 6 of 6

Sort Combobox list alphabetically

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Sort Combobox list alphabetically

    Hello,
    I am running this Combobox:

    Public Sub UserForm_Initialize()
    Dim ListItems As Collection, cell As Range
    On Error Resume Next
    Set ListItems = New Collection
    With ComboBox1
    .Clear
    For Each cell In Range("ListFruits")
    ListItems.Add cell.Value, cell.Value
       .AddItem cell.Value
    Next cell
    End With
    ComboBox1.ListIndex = 1
    UserForm1.Show
    End Sub
    I want to have the Combobox list displayed in alphabetical order, not in the order it is in the ListFruits.

    Could anyone advise please?
    Thank you

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Sort Combobox list alphabetically

    userform.show should not be part of the Initialize event.

    Public Sub UserForm_Initialize()
      combobox1.list=Range("ListFruits").value
    End Sub



  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sort Combobox list alphabetically

    Try
    Private Sub UserForm_Initialize()
    
        Dim oneCell As Range
        Dim i As Long
    
        ComboBox1.Clear
    
        For Each oneCell In Range("ListFruit")
            For i = 0 To ComboBox1.ListCount - 1
                If oneCell.Value < ComboBox1.List(i) Then Exit For
            Next i
            ComboBox1.AddItem oneCell.Value, i
        Next oneCell
    
        ComboBox1.ListIndex = 1
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Sort Combobox list alphabetically

    Hi mikerickson,
    Thanks for the suggestion; I copied the code into Sheet1 Code (attached);
    I am getting a Run-time error '1004';
    "Method "Range" of object '_Global' failed"
    Any idea why?
    Thanks
    Attached Files Attached Files

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sort Combobox list alphabetically

    Proabably because I used the named range ListFruit and the OP says ListFruits

  6. #6
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Sort Combobox list alphabetically

    Yes, Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sort ComboBox alphabetically
    By Onweerwolf in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-12-2014, 10:45 PM
  2. Sort combobox items alphabetically
    By aprilapple04 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-16-2013, 03:12 AM
  3. Sort combobox items alphabetically
    By VishalGupta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2011, 01:13 AM
  4. Replies: 2
    Last Post: 02-21-2006, 06:45 PM
  5. Sort text list alphabetically using a formula
    By paddyyates in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-20-2005, 11:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1