Consulting

Results 1 to 4 of 4

Thread: Combo box filtering?

  1. #1
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location

    Combo box filtering?

    I have the following scenario:

    A user selects a project in a combo box (cb_Proj).

    The projects dates range populates a second combo box (cb_ProjDate).

    I have been successful thus far. Now my issue:

    I would like the cb_ProjDate combo box to auto select (populate) with the next date that is less than today's date.

    Example:

    Today is 12/23/16.

    The selected projects date range is as follows (1/3/16, 5/3/16, 7/3/16, 1/1/17).

    I want the cb_ProjDate combo box to initial fill with the 7/3/16 date.

    Any help is greatly appreciated. Thank you in advance.

    Chunk

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Something like

    Dim Mem as Long, i As Long
    
    Mem = -1
    
    With ComboBox1
        For i = 1 To .ListCount - 1
            If CDate(.List(i)) < Date Then
                If mem = -1 Then mem = i
                If CDate(.List(mem)) < CDate(.List(i)) Then
                    mem = i
                End If
            End If
        Next i
    
        .ListIndex = mem
    End With

  3. #3
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location
    Mike,

    Here is the code I am using. When I inserted your code, I am receiving an "Object Required" error. The two combo boxes that I am working with are cbo_project and cb_DDate. Is it the method that I am using to populate the cb_DDate box? Any ideas?
        
        Dim ws As Worksheet
        Dim Rng As Range
        Dim FindString As String
        Dim Mem As Long, i As Long
     
        Mem = -1
            
        Set ws = ThisWorkbook.Worksheets(UserForm2.cbo_project.Text)
        
        Worksheets(UserForm2.cbo_project.Text).Activate
        
        'Populates dates dependant on project selected
        Range("L3", Range("L" & Rows.Count).End(xlUp)).Name = "Dynamic"
        
        UserForm2.cb_DDate.RowSource = "Dynamic"
        
        
        'Selects last date with data dependant on project selected
        
        With cb_DDate
            For i = 1 To .ListCount - 1
                If cb_DDate(.List(i)) < Date Then
                    If Mem = -1 Then Mem = i
                    If cb_DDate(.List(Mem)) < cb_DDate(.List(i)) Then
                        Mem = i
                    End If
                End If
            Next i
        
            .ListIndex = Mem
        End With
    Chunk

  4. #4
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location
    Sooooo,

    After a little fumbling around, I got it to work. Instead of declaring "i" as Long, I changed it to an Integer and it worked fine.

    A huge thanks to Mike for the quick assistance.

    Chunk

Posting Permissions

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