Filtering with a macro using input box + dates

olly_w

Board Regular
Joined
Jul 2, 2003
Messages
189
Hi guys
I've got a spreadsheet which in column 3, contains date and time e.g. 12/12/200 08:36 . What i want to do is have a macro embedded into a button which prompts the user for 2 dates, start and finish. The macro will then autofilter the records in the spreadsheet to only those in between the pre-entered dates. But my code isn't working, any ideas?


Sub PrintOutByMonth()
Dim FirstDate As Date
Dim LastDate As Date

Application.ScreenUpdating = False

FirstDate = Application.InputBox("Please Enter the First Date in DD MM YYYY hh:ss format: ", "Enter First Date")

LastDate = Application.InputBox("Please Enter the Last Date in DD MM YYYY format: ", "Enter Last Date")

Selection.AutoFilter Field:=1, Criteria1:=">=" & FistDate, Operator:=xlAnd _
, Criteria2:="<=" & LastDate

End Sub


Any help greatly appreciated, thanks in advance
olly
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Instead of:

Code:
Selection.AutoFilter Field:=1, Criteria1:=">=" & FirstDate, Operator:=xlAnd, Criteria2:="<=" & LastDate

try:

Code:
Selection.AutoFilter Field:=1, Criteria1:=">=" & CLng(FirstDate), Operator:=xlAnd, Criteria2:="<=" & CLng(LastDate)

Note that I corrected a typo in your code - FistDate to FirstDate. I hope you have Option Explicit at the top of your module and that the typo is only on the Board (not in your actual code).
 
Upvote 0
Sub PrintOutByMonth()
Dim FirstDate As Date
Dim LastDate As Date

Application.ScreenUpdating = False

FirstDate = Application.InputBox("Please Enter the First Date in DD MM YYYY hh:ss format: ", "Enter First Date")
LastDate = Application.InputBox("Please Enter the Last Date in DD MM YYYY format: ", "Enter Last Date")
Selection.AutoFilter Field:=3, Criteria1:=">=" & CLng(FirstDate), Operator:=xlAnd, Criteria2:="<=" & CLng(LastDate)
End Sub


This is my corrected code, option explicit at the top of the module. But when i run it, and i enter the dates, it doesn't return the correct records.
Any help greatly appreciated
cheers
olly
 
Upvote 0
ok i select the cells i wish to filter (ranging from dates 16/12/2003 to 24/12/2003), and then run the macro, and enter the dates 18/12/2003 to 25/12/2003. But it only returns the first record every time i.e. 16/12/2003.

Is it something to do with the fact that the contents of column C, which is the column i wish to filter on, look like 12/12/2003 08:28 i.e. include a time, but my input box does not cater for the time ?

Bit lost...
thanks in advance, your help is greatly appreciated
cheers
olly
 
Upvote 0
With this sample data:
BOOK9
ABCD
1Val1Val2Date
211016/12/030:00
321117/12/030:00
431218/12/030:00
541319/12/030:00
651420/12/030:00
761521/12/030:00
871622/12/030:00
981723/12/030:00
1091824/12/030:00
Sheet1


running your code and entering 18/12/03 and 25/12/03 resulted in rows 3 and 4 being hidden.

You should include a header row in your selection. The dates in column C are true serial dates aren't they? What happens if you filter the list for those dates manually?
 
Upvote 0
i've got my date time field formatted as custom dd/mm/yyyy hh:mm

but it just isn't finding the records ! -it only ever finds the first record, regardless of the dates input..
 
Upvote 0
olly_w said:
i've got my date time field formatted as custom dd/mm/yyyy hh:mm

So have I in my example.

olly_w said:
but it just isn't finding the records ! -it only ever finds the first record, regardless of the dates input..

It worked for me if I include the header in my selection.

In your case the first item is is being treated as a header. As I asked before:

1. The dates in column C are true serial dates aren't they?

2. What happens if you filter the list for those dates manually?
 
Upvote 0
Ok, the dates in column c are taken from excel from other cells using the ()now function, and formatted in the cell as a described earlier.
 
Upvote 0

Forum statistics

Threads
1,224,427
Messages
6,178,587
Members
452,860
Latest member
jroberts02

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