--- 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
|