What is the VBA code to Filter out the dates Less than year 2014?

sanmaya

Board Regular
Joined
Aug 1, 2012
Messages
82
Hi All,

Can anybody suggest me the VBA code to Filter the dates where the Year for the date is Less than 2014.

i have set of records and the date value is in column D of excel sheet. the format is the date values are dd-mmm-yy.i want to filter the records whose dates are before year 2014.

it will be appreciated .
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
i assume topleft cell is A1 and date format in international settings is european date format (DMY)

i'm copying 2 codes that i use regularly for different purposes.

Code:
Sub FilterOnDateCopy()

    Dim CritDate As Date
    CritDate = DateSerial(2014, 1, 1)
    
    'Option 1: Copy records before 2014 to new sheet
    With Worksheets("MySheet") 'change sheet name to suit
        .AutoFilterMode = False
        .Cells(1).AutoFilter Field:=4, Criteria1:="<" & CLng(CritDate)
        .AutoFilter.Range.Copy Destination:=Worksheets("NewSheet").Range("A1")
        .AutoFilterMode = False
    End With
    
End Sub

Code:
Sub FilterOnDateCopyDelete()

    Dim CritDate As Date
    CritDate = DateSerial(2014, 1, 1)
    
    'Option 2: Copy records before 2014 to new sheet and delete
    With Worksheets("MySheet") 'change sheet name to suit
        .AutoFilterMode = False
        .Cells(1).AutoFilter Field:=4, Criteria1:="<" & CLng(CritDate)
        .AutoFilter.Range.Copy Destination:=Worksheets("NewSheet").Range("A1")
        .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete
        .AutoFilterMode = False
    End With

End Sub
 
Last edited:
Upvote 0
i assume topleft cell is A1 and date format in international settings is european date format (DMY)

i'm copying 2 codes that i use regularly for different purposes.

Code:
Sub FilterOnDateCopy()

    Dim CritDate As Date
    CritDate = DateSerial(2014, 1, 1)
    
    'Option 1: Copy records before 2014 to new sheet
    With Worksheets("MySheet") 'change sheet name to suit
        .AutoFilterMode = False
        .Cells(1).AutoFilter Field:=4, Criteria1:="<" & CLng(CritDate)
        .AutoFilter.Range.Copy Destination:=Worksheets("NewSheet").Range("A1")
        .AutoFilterMode = False
    End With
    
End Sub

Code:
Sub FilterOnDateCopyDelete()

    Dim CritDate As Date
    CritDate = DateSerial(2014, 1, 1)
    
    'Option 2: Copy records before 2014 to new sheet and delete
    With Worksheets("MySheet") 'change sheet name to suit
        .AutoFilterMode = False
        .Cells(1).AutoFilter Field:=4, Criteria1:="<" & CLng(CritDate)
        .AutoFilter.Range.Copy Destination:=Worksheets("NewSheet").Range("A1")
        .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete
        .AutoFilterMode = False
    End With

End Sub

Thanks a Lot for your Help
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,036
Members
449,205
Latest member
Eggy66

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