+ Reply to Thread
Results 1 to 5 of 5

Excel 2007 sort combobox

Hybrid View

  1. #1
    Registered User
    Join Date
    05-19-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Post Excel 2007 sort combobox

    Hi everyone, this is my first post, and I am NOT a proficient VBA programmer. For some of you this might be easy, but after 2 days, it is driving me nuts.

    I have a combobox which is populated with folder names,, the folder names are month and year. eg Apr 2013, this works fine, no problems. The problem is, in the combobox list the months are in alphabetical order, I would like to sort them so that they run from Jan 2013 to Dec 2013.

    All help appreciated, but PLEASE keep it relevant.

    My code:

    Private Sub CommandButton1_Click()
    Dim name


    Me.ComboBox2.Clear
    For Each name In ListDirectory(Path:="C:\Users\Laurie\Documents\Pai Hotel\Log\Daily Log - Year 2013\", AttrInclude:=vbDirectory, AttrExclude:=vbSystem Or vbHidden)
    If name Like "Jan*" Or name Like "Feb*" Or name Like "Mar*" Or name Like "Apr*" Or name Like "May*" Or name Like "Jun*" _
    Or name Like "Jul*" Or name Like "Aug*" Or name Like "Sep*" Or name Like "Oct*" Or name Like "Nov*" Or name Like "Dec*" Then

    Me.ComboBox2.AddItem name
    End If
    Next name

    End Sub
    Last edited by LaurieH; 05-20-2013 at 05:06 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Excel 2007 sort combobox

    Hi LaurieH,
    maybe something like this
    Private Sub CommandButton1_Click()
    Dim arr, i As Long, nm
    arr = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    For Each nm In ListDirectory(Path:="C:\Users\Laurie\Documents\Pai Hotel\Log\Daily Log - Year 2013\", AttrInclude:=vbDirectory, AttrExclude:=vbSystem Or vbHidden) '??
        For i = LBound(arr) To UBound(arr)
            If InStr(nm, arr(i)) Then arr(i) = nm: Exit For
        Next
    Next nm
    Me.ComboBox2.Clear
    For Each nm In arr
        If Len(nm) > 3 Then Me.ComboBox2.AddItem nm
    Next nm
    End Sub

  3. #3
    Registered User
    Join Date
    05-19-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excel 2007 sort combobox

    nlerm, thanks for your very quick reply, your code is exactly what I needed, I have a lot to learn .

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

    Re: Excel 2007 sort combobox

    You can fill the comobox with montnames:

    sub M_snb()
      combobox1.list=Application.GetCustomListContents(3)
    end sub
    After selection you can open the file using:

    Private Sub Combobox1_change()
      If combobox1.listindex<>-1 then workbooks.open "C:\Users\Laurie\Documents\Pai Hotel\Log\Daily Log - Year 2013\" & dir("C:\Users\Laurie\Documents\Pai Hotel\Log\Daily Log - Year 2013\*" & combobox1.value & "*")
    End Sub
    Last edited by snb; 05-19-2013 at 08:56 AM.



  5. #5
    Registered User
    Join Date
    05-19-2013
    Location
    Thailand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excel 2007 sort combobox

    snb, thanks for you quick reply, the first part you supplied isn't quite what I wanted, BUT, the second part I can use, thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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