Ask Experts Questions for FREE Help !
Ask
    HEriksen's Avatar
    HEriksen Posts: 2, Reputation: 1
    New Member
     
    #1

    Oct 19, 2011, 02:34 AM
    "Is before or equal to" criteri to filter via macro in Excel 2010
    I have a problem in a VBA code in Excel 2010.
    I try to set a date filter criteria, which is not "equal to", but "is before or equal to" a date defined in a variable named 'Crit_Date'. I use the following VBA line:

    Selection.AutoFilter field:=6, Criteria1:="<=" & Crit_Date

    The problem is that the filter doesn't show any results even though there are valid data for that criteria.

    When I afterwards (after running the macro) go to Excel and press the filter button, and click on "Date Filters" and click on "Custom Filter...", I see that the filter is defined correctly by the macro. When I then hit the "OK" button, the filter seems to work fine.

    So the question is: How can I set a criteria using a macro, which doesn't work when running from the macro even though it is set correctly? And what can I do?
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Oct 19, 2011, 11:31 PM
    I've frequently seen problems in VBA Autofiltering a DATE column. Typically, I insure the date is formatted exactly the same as the column, that eliminates most of the problems.

    Also, you should eliminate the "selecting" part of the macro, selecting and activating are human requirements for working on a sheet, not VBA requirements. You can send Autofilter commands directly to a sheet without ever activating it:

    Code:
    With Sheets("Sheet1")
        .AutoFiltermode = False    'remove prior filters
        .Rows(1).AutoFilter        'turn on new filter
        .Rows(1).AutoFilter Field:=6, Criteria1:="<=" & Format(Crit_Date, "MM-DD-YYYY")
    End With
    HEriksen's Avatar
    HEriksen Posts: 2, Reputation: 1
    New Member
     
    #3

    Oct 24, 2011, 03:04 AM
    Thanks, but it didn't solve my problem.
    The strange thing about this is, that when I look in the defined filters after running the macro, the date criteria is set perfectly fine and works fine if I then pross OK. But it doesn't work directly from the macro.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Macro to sniff out "#N/A'" errors and change them to blank. [ 14 Answers ]

I have macros running but sometimes #N/A errors stop the running macro and I get lost with the current update. I need a macro that will run through the sheet and change all #N/A errors to a blank cell. Please find attached a sheet showing my problem. Many thanks.

"Form" placed in "Microsoft Access" can be accessed from a "Button" in "VB.Net" App [ 1 Answers ]

Hi All, Actually, I'm not very well in programming but a task is assigned to me related to .Net. Basically, there is a database in Microsoft Access. I have made forms in it which are based on queries to retrieve required results. I have also made graph of it. Now, I have to merge this...

Excel Filter Macro needed [ 10 Answers ]

I have a list and want to filter it with reference to a cell. e.g. list is ranging from Row 5 to 2000, I want to assigne Cell A4 as reference to filter the data. When something is entered in A4 then it should check that A5 to A2000 has that data or not if yes then filter or else don't execute and...


View more questions Search