+ Reply to Thread
Results 1 to 6 of 6

subscript out of range combo box auto filter selection

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-04-2011
    Location
    pak
    MS-Off Ver
    Excel 2007
    Posts
    132

    subscript out of range combo box auto filter selection

    Dear experts,
    I am working the following code. combobox1 has months jan~dec in it. combobox2 has years 2007~2009.upon selection change in combobox the auto filter should be applied according to given criteria in the code, but I am getting subscript out of range in final code line auto filter. Any idea?

    thanks

    Private Sub ComboBox1_Click()
    If ComboBox2.Value = "" Then Exit Sub
    Call FilterDates
    End Sub
    
    
    
    Private Sub ComboBox2_Click()
    If ComboBox1.Value = "" Then Exit Sub
    Call FilterDates
    End Sub
    
    
    
    
    Private Sub FilterDates()
    Dim iStartMonth As Integer
    Dim iStartYear As Integer
    Dim dteStartDate As Date
    Dim dteEndDate As Date
    Dim sStartCriterion As String
    Dim sEndCriterion As String
    'Get Date values
    iStartMonth = Me.ComboBox1.ListIndex + 1
    iStartYear = Me.ComboBox2.Value
    'Calculate date values and format as US Dates
    dteStartDate = DateSerial(iStartYear, iStartMonth, 1)
    dteEndDate = DateSerial(iStartYear, iStartMonth + 1, 1)
    sStartCriterion = " >= " & Format(dteStartDate, "mm / dd / yyyy")
    sEndCriterion = " < " & Format(dteEndDate, "mm / dd / yyyy")
    'Apply AutoFilter
    Me.ListObjects("Table1").Range.AutoFilter _
    Field:=1, _
    Criteria1:=sStartCriterion, _
    Operator:=xlAnd, _
    Criteria2:=sEndCriterion
    End Sub
    Last edited by shaukat; 12-17-2011 at 06:19 AM.

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: subscript out of range combo box auto filter selection

    Try referencing the sheet indstead og "Me.Listobjects"
    Please take time to read the forum rules

  3. #3
    Forum Contributor
    Join Date
    11-04-2011
    Location
    pak
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: subscript out of range combo box auto filter selection

    Dear Steffen
    Thanks for your suggestion, I did it but same result. my sheet is attached.Please view it and give your advice

    thanks
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: subscript out of range combo box auto filter selection

    2 things

    Private Sub FilterDates()
    Dim iStartMonth As Integer
    Dim iStartYear As Integer
    Dim dteStartDate As Date
    Dim dteEndDate As Date
    Dim sStartCriterion As String
    Dim sEndCriterion As String
    'Get Date values
    iStartMonth = Me.ComboBox1.ListIndex + 1
    iStartYear = Me.ComboBox2.Value
    'Calculate date values and format as US Dates
    dteStartDate = DateSerial(iStartYear, iStartMonth, 1)
    dteEndDate = DateSerial(iStartYear, iStartMonth + 1, 1)
    sStartCriterion = ">=" & CDate(Format(dteStartDate, "mm-dd-yyyy"))
    sEndCriterion = "<" & CDate(Format(dteEndDate, "mm-dd-yyyy"))
    'Apply AutoFilter
    Sheets(1).ListObjects("Table2").Range.AutoFilter _
    Field:=1, _
    Criteria1:=sStartCriterion, _
    Operator:=xlAnd, _
    Criteria2:=sEndCriterion
    End Sub
    You need to change the format as seen here
    And secondly, your table is named "Table2" not "Table1"

    One last thing, i suggest you lock your comboboxes, else they will move with the autofilter

  5. #5
    Forum Contributor
    Join Date
    11-04-2011
    Location
    pak
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: subscript out of range combo box auto filter selection

    thanks for correcting me.You solved my case. One thing how to lock combobox.

  6. #6
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: subscript out of range combo box auto filter selection

    Right click on the combobox while in design mode and then press "Format control", here you go to the tab "Properties" and select "Don't move or size with cells".

+ 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