1

I have column of dates, which is an output from a database. There is a macro that filters on this date which is meant to show all dates older than 14 days from current date excluding nulls. The filter when applied filters the entire sheet and shows nothing. When I take off the - 14 I do get dates, I have tried even using alt a, e, f to ensure that the column is the correct format, but no luck.

Here is my code snippet:

ActiveSheet.UsedRange.AutoFilter Field:=34, Criteria:="<>NULL", _
Operator:=xlAnd, Criteria2:="<" & Now()-14

I have tried changing the criteria2 to "<" & Date - 14 & "# 00:00:00 AM#"

Please help

1 Answer 1

1

Try this

Change Criteria:="<>NULL" to Criteria1:="<>NULL"

FOLLOWUP

The code was not working as the headings were in not in Row 1. Once the exact range was specified, it started working.

Option Explicit

Sub Sample()
    Dim lRow As Long

    lRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

    ActiveSheet.AutoFilterMode = False

    ActiveSheet.Range("$A$2:$BV$" & lRow).AutoFilter Field:=34, Criteria1:="<>NULL" _
    , Operator:=xlAnd, Criteria2:="<" & Format(Date - 14, "0")
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, that's a typo error on my part as I was typing it was another screen, but that is not the error
In that case check the format of the date values in the Excel sheet. Hope they are not stored as text? I have tested your above code and it works.
If it still doesn't help then can I see a sample as well if possible
What is the best to check this, because all I know it is from an Oracle database in the following format = dd/mm/yyyy HH:MM and what is the best way to solve this
How do you want me to give the sample date? All the date is 06/04/2011 07:17
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.