+ Reply to Thread
Results 1 to 8 of 8

Filter Date Field by month using VBA

  1. #1
    Stuart Grant
    Guest

    Filter Date Field by month using VBA

    I have a list with transactions. The first field is Date (maybe that's not
    a good name) and I want to filter the records by month.

    I tried this
    Mon = Val(InputBox("Which month"))
    Selection.AutoFilter Field:=1 Criteria:= "Month(Field 1) = Mon"
    but this gives a syntax error as do several variations which I have tried,
    although Month(Date) is apparently a VBA function.

    Can anyone suggest the right syntax ?

    Stuart



  2. #2
    Ardus Petus
    Guest

    Re: Filter Date Field by month using VBA

    I don't think you can do that with autofilter.
    You'll have to use an advanced filter with a critaria range

    HTH
    --
    AP

    "Stuart Grant" <sagrantatbluewindotch> a écrit dans le message de
    news:[email protected]...
    > I have a list with transactions. The first field is Date (maybe that's

    not
    > a good name) and I want to filter the records by month.
    >
    > I tried this
    > Mon = Val(InputBox("Which month"))
    > Selection.AutoFilter Field:=1 Criteria:= "Month(Field 1) = Mon"
    > but this gives a syntax error as do several variations which I have tried,
    > although Month(Date) is apparently a VBA function.
    >
    > Can anyone suggest the right syntax ?
    >
    > Stuart
    >
    >




  3. #3
    Roger Govier
    Guest

    Re: Filter Date Field by month using VBA

    Hi

    Autofilter will not work like that, it needs to use the date range for
    the month combined with 2 criteria "And'd" together.
    You will need to convert your month input into the two date to be used
    for Criter1a1 and Criteria2

    Selection.AutoFilter
    Selection.AutoFilter Field:=1, Criteria1:=">=01/01/2006",
    Operator:=xlAnd _
    , Criteria2:="<=31/01/2006"

    Alternatively, on the Sheet of data, add an extra column, say column J,
    with the formula =MONTH(A1) copied down and then apply your single
    filter to that new field

    Mon = Val(InputBox("Which month"))
    Selection.AutoFilter Field:=10 Criteria:= Mon

    --
    Regards

    Roger Govier


    "Stuart Grant" <sagrantatbluewindotch> wrote in message
    news:[email protected]...
    >I have a list with transactions. The first field is Date (maybe that's
    >not a good name) and I want to filter the records by month.
    >
    > I tried this
    > Mon = Val(InputBox("Which month"))
    > Selection.AutoFilter Field:=1 Criteria:= "Month(Field 1) = Mon"
    > but this gives a syntax error as do several variations which I have
    > tried, although Month(Date) is apparently a VBA function.
    >
    > Can anyone suggest the right syntax ?
    >
    > Stuart
    >




  4. #4
    Stuart Grant
    Guest

    Re: Filter Date Field by month using VBA

    Roger
    Many thanks. Your alternative suggestion looked fine - very compact, but I
    can't get it to work.
    I made an extra Column I with =Month(A1) copied all the way down but
    Selection.AutoFilter Field:= 9, Criteria:=Mon
    gives Error 1004 - Application Defined or Object Defined Error - which I
    always think is one of the most unhelpful messages around but never mind.
    I have checked over and over for typos but found none. I think actually you
    missed a comma but I tried leaving it out. No go.
    Stuart

    "Roger Govier" <[email protected]> wrote in message
    news:e2Ac%[email protected]...
    > Hi
    >
    > Alternatively, on the Sheet of data, add an extra column, say column J,
    > with the formula =MONTH(A1) copied down and then apply your single filter
    > to that new field
    >
    > Mon = Val(InputBox("Which month"))
    > Selection.AutoFilter Field:=10 Criteria:= Mon
    >
    > --
    > Regards
    >
    > Roger Govier
    >>

    >
    >




  5. #5
    Roger Govier
    Guest

    Re: Filter Date Field by month using VBA

    Hi Stuart
    Do you have autofilter applied to all columns from A to I?
    If not, count the position of the autofilter for column I and substitute
    in place of 9 in the code.
    Check that Mon is an integer value between 1 and 12.

    --
    Regards

    Roger Govier


    "Stuart Grant" <sagrantatbluewindotch> wrote in message
    news:[email protected]...
    > Roger
    > Many thanks. Your alternative suggestion looked fine - very compact,
    > but I can't get it to work.
    > I made an extra Column I with =Month(A1) copied all the way down but
    > Selection.AutoFilter Field:= 9, Criteria:=Mon
    > gives Error 1004 - Application Defined or Object Defined Error - which
    > I always think is one of the most unhelpful messages around but never
    > mind. I have checked over and over for typos but found none. I think
    > actually you missed a comma but I tried leaving it out. No go.
    > Stuart
    >
    > "Roger Govier" <[email protected]> wrote in message
    > news:e2Ac%[email protected]...
    >> Hi
    >>
    >> Alternatively, on the Sheet of data, add an extra column, say column
    >> J, with the formula =MONTH(A1) copied down and then apply your single
    >> filter to that new field
    >>
    >> Mon = Val(InputBox("Which month"))
    >> Selection.AutoFilter Field:=10 Criteria:= Mon
    >>
    >> --
    >> Regards
    >>
    >> Roger Govier
    >>>

    >>
    >>

    >
    >




  6. #6
    Stuart Grant
    Guest

    Re: Filter Date Field by month using VBA

    Roger
    Yes. autofilter is applied to all columns and the Month column is I or 9.
    Debug-AddWatch confirms Mon is an integer - in my trials 2.
    I'm baffled.
    I have to stop now but this afternoon I'll record a macro doing it manually
    and see what comes.
    Stuart

    "Roger Govier" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Stuart
    > Do you have autofilter applied to all columns from A to I?
    > If not, count the position of the autofilter for column I and substitute
    > in place of 9 in the code.
    > Check that Mon is an integer value between 1 and 12.
    >
    > --
    > Regards
    >
    > Roger Govier




  7. #7
    Stuart Grant
    Guest

    Re: Filter Date Field by month using VBA

    Roger
    Oh silly me! As you will see in my previous messages. I had -
    Selection.autofilter Fields:= 9, Criteria = Mon
    It has to be Criteria1 not just Criteria, even if there is only one.
    Works perfectly now. Thanks for your help.
    Stuart


    "Stuart Grant" <sagrantatbluewindotch> wrote in message
    news:[email protected]...
    > Roger
    > Yes. autofilter is applied to all columns and the Month column is I or 9.
    > Debug-AddWatch confirms Mon is an integer - in my trials 2.
    > I'm baffled.
    > I have to stop now but this afternoon I'll record a macro doing it
    > manually and see what comes.
    > Stuart




  8. #8
    Roger Govier
    Guest

    Re: Filter Date Field by month using VBA

    Hi Stuart

    You're not the silly one - it was me.
    In copying and pasting, I wiped out the 1 after criteria in my posting
    to you.
    Glad you spotted it and got it all resolved.

    --
    Regards

    Roger Govier


    "Stuart Grant" <sagrantatbluewindotch> wrote in message
    news:[email protected]...
    > Roger
    > Oh silly me! As you will see in my previous messages. I had -
    > Selection.autofilter Fields:= 9, Criteria = Mon
    > It has to be Criteria1 not just Criteria, even if there is only one.
    > Works perfectly now. Thanks for your help.
    > Stuart
    >
    >
    > "Stuart Grant" <sagrantatbluewindotch> wrote in message
    > news:[email protected]...
    >> Roger
    >> Yes. autofilter is applied to all columns and the Month column is I
    >> or 9.
    >> Debug-AddWatch confirms Mon is an integer - in my trials 2.
    >> I'm baffled.
    >> I have to stop now but this afternoon I'll record a macro doing it
    >> manually and see what comes.
    >> Stuart

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1