Filter a Range, based on combobox value

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet with data for each day of the year. I want to use a combobox, in order to filter these data for a particular month.

So when the value in the combox changes, then the list needs to be filtered accordingly.

Thanks in advance.
 

Attachments

  • filter_combobox.png
    filter_combobox.png
    48.6 KB · Views: 12

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
For a form control combobox, assign this macro to it:
VBA Code:
Public Sub Filter_By_Month()

    Dim monthCombo As DropDown

    With ActiveSheet
        Set monthCombo = .DropDowns(1)
        .Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(DateSerial(Year(.Range("A2").Value), monthCombo.Value, 1)), _
                                       Operator:=xlAnd, Criteria2:="<=" & CLng(DateSerial(Year(.Range("A2").Value), monthCombo.Value + 1, 0))
    End With
    
End Sub
 
Upvote 0
Eventually, I worked on a solution as below, and it works. Comments/Suggestions for improvement are welcome.

VBA Code:
Private Sub monthCombo_Click()
Dim montH As String
Dim StartDate As String, EndDate As String

montH = monthCombo.Value
Select Case montH
    Case "January"

        'StartDate = DateSerial(2023, 1, 1)   this does not work, I defined the dates as below in the Select block
        'EndDate = DateSerial(2023, 12, 31)

        StartDate = "1/1/2023"
        EndDate = "1/31/2023"
    Case "February"
        StartDate = "2/1/2023"
        EndDate = "2/28/2023"
    Case "March"
        StartDate = "3/1/2023"
        EndDate = "3/31/2023"
    Case "April"
        StartDate = "4/1/2023"
        EndDate = "4/30/2023"
    Case "May"
        StartDate = "5/1/2023"
        EndDate = "5/31/2023"
    Case "June"
        StartDate = "6/1/2023"
        EndDate = "6/30/2023"
    Case "July"
        StartDate = "7/1/2023"
        EndDate = "7/31/2023"
    Case "August"
        StartDate = "8/1/2023"
        EndDate = "8/31/2023"
    Case "September"
        StartDate = "9/1/2023"
        EndDate = "9/30/2023"
    Case "October"
        StartDate = "10/1/2023"
        EndDate = "10/31/2023"
    Case "November"
        StartDate = "11/1/2023"
        EndDate = "11/30/2023"
    Case "December"
        StartDate = "12/1/2023"
        EndDate = "12/31/2023"
End Select

ActiveSheet.Range("$A$1:$C366").AutoFilter Field:=1, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate

End Sub
 
Upvote 0
For an ActiveX combobox, which you seem to be using, named monthCombo, the ListIndex + 1 property gives the month number. Note I've used the Change event, rather than the Click event, so that the data is filtered only when you change the month.
VBA Code:
Private Sub monthCombo_Change()
    With ActiveSheet
        .Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(DateSerial(Year(.Range("A2").Value), monthCombo.ListIndex + 1, 1)), _
                                       Operator:=xlAnd, Criteria2:="<=" & CLng(DateSerial(Year(.Range("A2").Value), monthCombo.ListIndex + 2, 0))
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top