Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can combo box contents be sorted A-Z at run time?

    I have a simple drop down combo box with a list of names. When user views the list of drop down items, not all display in A-Z order. The items are in A-Z order in the Initialize code but do not display like that in the drop down. Is there a simple way to sort the list at run time? One suggestion I found was to write an SQL query, but I've never written one before. Using Word 2010 but form may be used with XP.

    Example:

    The Initialize code displays:
    Private Sub UserForm_Initialize()

    With cboWhat
    .AddItem "Cats"
    .AddItem "Dogs"
    .AddItem "Lizards"
    End With
    End Sub

    The User views these items in the drop down list for selection:
    Dogs
    Cats
    Lizards

    Thanks for your help!
    Charlie
    charlie6067
    Last edited by charlie6067; 2014-10-28 at 15:20. Reason: Added Word version

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    I have never encountered a combo box that, when populated via .AddItem, listed the items in any order other than that in which they were added. Your code snippet works as expected - the items appear in the order in which they were added in both Office 2003 & Office 2010.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Charlie

    This is an odd problem. On my Word 2010 machine I get the list in the order that the additems are listed so I don't know why they might appear in a different order on your machine.

    Try the following code variation to see if you can get the desired result
    Code:
      Dim arrWhat() As String
      arrWhat = Split("Lizards,Cats,Dogs,Zebras,Lions", ",")
      WordBasic.sortArray arrWhat()
      cboWhat.List = arrWhat
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Tags for this Thread

Posting Permissions

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