Filter date range by selecting the month

rasika_99

New Member
Joined
Mar 4, 2024
Messages
2
Office Version
  1. 2007
Platform
  1. Windows
I have an Excel file with a Table. It contains a column that contains the expiry dates of policies. I have a dropdown menu above the table containing the all months (Jan –Dec). When I select a month, I need to filter the records relating to that month and copy to a new sheet.

How can I write a vba code for this?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You've provided very little information to work with, so the following is provided for demonstration purposes only. It makes lots of assumptions - you have an actual table (not just a range) and the data is set out as shown below. You'll have to change everything to fit your actual data layout.

Running this code:

VBA Code:
Option Explicit
Sub FilterTableCopy()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")          '<-- *** Change sheet names to suit ***
    Set ws2 = Worksheets("Sheet2")
    Dim Mnth As Long
    Mnth = Application.Evaluate("=MONTH(1&" & Chr(34) & ws1.[B1] & Chr(34) & ")") + 20
    
    With ws1.ListObjects("Table1")
        .Range.AutoFilter 2, Mnth, 11
        If WorksheetFunction.Subtotal(3, ws1.Range("B3", ws1.Cells(Rows.Count, "B").End(xlUp))) > 1 Then
            .DataBodyRange.SpecialCells(xlCellTypeVisible).Copy ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        Else
            MsgBox "No records found for this month"
            .AutoFilter.ShowAllData
            Exit Sub
        End If
        .AutoFilter.ShowAllData
    End With
End Sub

With this on sheet 1:

Filter Table.xlsm
AB
1Month:Jul
2
3Policy #Expiry date
4101531-Aug-24
5109225-Jul-24
6109203-Jan-24
7107601-Sep-24
8101029-May-24
9103902-Jun-24
10108619-Sep-24
11102024-Apr-24
12108327-Apr-24
13108224-May-24
14100903-Sep-24
15108131-Jul-24
16103126-May-24
17107815-Jul-24
18107820-Nov-24
19103011-Sep-24
20104902-Jun-24
21108830-Oct-24
22103604-Sep-24
23106906-Jun-24
Sheet1
Cells with Data Validation
CellAllowCriteria
B1ListJan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec


Will give you this on sheet 2 (headers already in place):

Filter Table.xlsm
AB
1Policy #Expiry date
2109225-Jul-24
3108131-Jul-24
4107815-Jul-24
5
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,150
Members
453,021
Latest member
Justyna P

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