Date Comparisson Code

Craw

New Member
Joined
Jul 27, 2011
Messages
32
Greetings,
I am trying to write code for a command button that will compare two dates. I have a spreadsheet with a list of documents and their approval dates. The approval dates are listed in a column and I have an autofilter setup in said column. What I would like to do is push a button and have have the autofilter compare the current date to that of the documents approval date. Only the documents that are more then 1 year old should show up in the autofilter.

Any ideas?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
So far I have had help to develop a code that searches a user specified date range. I would like to modify this code so that when the button is clicked it will automatically check the current date and look for records that go back 1 year or more. Is this possible?

Code:
Private Sub CommandButton13_Click()
 
Dim strName1 As Variant, strName2 As Variant
Dim caption As String
Dim Prompt As String
 
Prompt = "Enter the Date Range in the 'dd/mm/yyyy' format?"
caption = "Approval Date Range Search"
On Error GoTo Errorline
 
strName1 = DateValue(Application.InputBox(Prompt & " Enter 'START' Date", caption, Type:=2))
strName2 = DateValue(Application.InputBox(Prompt & " Enter 'END' Date", caption, Type:=2))
 
If strName1 = "False" Or strName2 = "False" Then Exit Sub
Range("Table_owssvr_1[Approval Date]").Select
Selection.AutoFilter Field:=6, Criteria1:=">=" & CLng(strName1), Criteria2:="<=" & CLng(strName2), Operator:=xlAnd
Exit Sub
 
Errorline:
ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6
End Sub
 
Upvote 0
Try something like this (not tested). Change the name in red to the name of the button this is assigned to.

Code:
Private Sub [COLOR="Red"]CommandButton13[/COLOR]_Click()

    On Error GoTo Errorline

    Range("Table_owssvr_1[Approval Date]").Select
    Selection.AutoFilter Field:=6, Criteria1:="<=" & DateAdd("yyyy", -1, Date)
    Exit Sub

Errorline:
    ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6
    
End Sub
 
Upvote 0
Code:
    On Error GoTo Errorline

    Range("Table_owssvr_1[Approval Date]").Select
    [COLOR="Red"]Selection.AutoFilter[/COLOR]    [COLOR="Green"]'clear previous filter[/COLOR]
    Selection.AutoFilter Field:=6, Criteria1:="<=" & DateAdd("yyyy", -1, Date)
    Exit Sub

Errorline:
    ActiveSheet.ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,923
Latest member
JackiG

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