Excel

Autofiltering with date

Ease of Use

Easy

Version tested with

2003 

Submitted by:

Charlize

Description:

Quick way to view rows with a date until now (current date not included). 

Discussion:

Need a quick routine to filter on records that are due until now. Always use the current date (system date) to filter. 

Code:

instructions for use

			

--- put following code In a module without the --- marks Option Explicit Sub filter_less_then_today() ' declare as date Dim dDate As Date ' declare as long (using numbering of excel for dates) Dim lDate As Long ' store answer to msgbox Dim answer As Long ' store today in datevariabele dDate = DateSerial(year(Now), month(Now), day(Now)) ' save as number lDate = dDate ' answer to question If Worksheets(1).AutoFilterMode = True Then answer = MsgBox("Turn autofilter off (yes)" & vbCrLf & _ "Filter on date until now (no)" & vbCrLf, vbYesNoCancel, "Filtering ...") Select Case answer ' yes Case 6 Range("B1").AutoFilter ' no Case 7 ' cancel Case 2 End Select Else ' apply filter yes or no answer = MsgBox("Filter on date until now (yes)" & vbCrLf & _ "Do nothing and leave it as it is (no)" & vbCrLf, vbYesNo, "Filtering ...") Select Case answer ' yes Case 6 Range("B1").AutoFilter field:=1, Criteria1:="<" & lDate ' no Case 7 End Select End If End Sub --- End of code module

How to use:

  1. alt+f11
  2. right click on left side of screen (project window)
  3. insert module and paste code into newly created module
  4. close vb editor
  5. run with alt+F8 and choose filter_less_than_today
 

Test the code:

  1. Add some dates in column B before and after current date.
 

Sample File:

VBAX - autofilter with dates - v2.zip 9.86KB 

Approved by mdmackillop


This entry has been viewed 391 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express