VBA Filter on Date

SAldrich

New Member
Joined
Sep 29, 2008
Messages
15
Hello everyone,

I have a macro which I want to filter data against a date which the user enters then delete the visible rows however when I run this, it doesn't return anything in the filter so I think it is not recognising the criteria as a date. Any ideas on how to resolve this?

Thanks for your help!!


Sub Delete_old()

Dim CutOffDate As Date

CutOffDate = Application.InputBox("Enter the cut off date in the Format ""DD/MM/YYYY""")

Selection.AutoFilter
Selection.AutoFilter Field:=17, Criteria1:="<=" & CutOffDate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
Range("A1").Select

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi

try this:

Code:
Sub Delete_old()

Dim CutOffDate As Long
Dim temp As String

 temp = Application.InputBox("Enter the cut off date in the Format ""DD/MM/YYYY""")
 
CutOffDate = CLng(DateValue(temp))

Selection.AutoFilter
Selection.AutoFilter Field:=17, Criteria1:="<=" & CutOffDate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
Range("A1").Select

End Sub
 
Upvote 0
Hi I am using the below code to filter data between. The issue i am facing is , when data is entered to any cell in the workbook the filters refresh happens. I am looking for teh filter to refresh only when the data in the cell C1 or D1 is changed.

Private Sub Worksheet_Change(ByVal Target As Range)


Dim StartDate As Long
Dim EndDate As Long



StartDate = Range("C1").Value
EndDate = Range("D1").Value


Range("A3").Select
Selection.AutoFilter
Selection.AutoFilter Field:=6, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate






End Sub
 
Upvote 0
Re: VBA Filter on 2 dates entered by user

Hello everyone,

I have a macro which I want to filter data against 2 dates which the user enters. Any ideas on how to resolve this?

User will enter 2 dates in Input Box and macro should filter date basis 2 dates entered. Dates are stored in column D of worksheet "Copy"

I am in urgent need of same so please assist on same quickly.

Thanks for your help in advance!!
 
Last edited:
Upvote 0
Re: VBA Filter on 2 dates entered by user

Hi ,

Did you get answer for this.

even i have same problem.

if you resolved can you please share the code as soon as possible.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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