+ Reply to Thread
Results 1 to 8 of 8

macro's problem

  1. #1
    gerry405
    Guest

    macro's problem


    Firstly, excuse my ignorance in advance to those who make coding in VB
    look so easy!

    Hi everyone

    Can anyone show me how to filter on a column containing dates in the
    format of 21/04/59 being (21st April 1959), I set about recording a
    macro that first put a filter on the worksheet, then I selected the
    column with dates in and go to custom filter then select "equal to or
    lesser than" and key in the value 30/09/2005, but all that happens when
    I run this is I get blank lines ...

    code below

    Sub less_than()
    '
    ' less_than Macro
    ' Macro recorded 07/10/2005 by SGUHT
    '
    ' Keyboard Shortcut: Ctrl+k
    '
    Selection.AutoFilter
    Range("U1").Select
    Selection.AutoFilter Field:=21, Criteria1:="<=30/09/2005",
    Operator:= _
    xlAnd
    End Sub


    --
    gerry405

  2. #2
    Forum Contributor
    Join Date
    01-11-2004
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    388
    Hi!

    When Excel stores a date it does it as an integer counting from 1 Jan 1900 (=1) normally.

    Your comparison needs to be done using similar numbers.

    Put 30/09/2005 in a cell and it will look just like that.
    Now reformat it as General and it will read 38625.
    This gives you a simple way of "translating" between the number as stored and the date as viewed.

    That's fine on the worksheet but you need the integer form in the VBA.

    Alf

  3. #3
    Dave Peterson
    Guest

    Re: macro's problem

    Dates can be a problem in VBA.

    This may work:

    Selection.AutoFilter Field:=21, Criteria1:="<="&clng(dateserial(2005,9,30))



    gerry405 wrote:
    >
    > Firstly, excuse my ignorance in advance to those who make coding in VB
    > look so easy!
    >
    > Hi everyone
    >
    > Can anyone show me how to filter on a column containing dates in the
    > format of 21/04/59 being (21st April 1959), I set about recording a
    > macro that first put a filter on the worksheet, then I selected the
    > column with dates in and go to custom filter then select "equal to or
    > lesser than" and key in the value 30/09/2005, but all that happens when
    > I run this is I get blank lines ...
    >
    > code below
    >
    > Sub less_than()
    > '
    > ' less_than Macro
    > ' Macro recorded 07/10/2005 by SGUHT
    > '
    > ' Keyboard Shortcut: Ctrl+k
    > '
    > Selection.AutoFilter
    > Range("U1").Select
    > Selection.AutoFilter Field:=21, Criteria1:="<=30/09/2005",
    > Operator:= _
    > xlAnd
    > End Sub
    >
    > --
    > gerry405


    --

    Dave Peterson

  4. #4
    Registered User
    Join Date
    10-07-2005
    Posts
    4

    macro

    Dave,


    thank you for your quick response, that code worked a treat...

    Alf,

    You answered another question that was bugging me, about Dates/Formating...I thought at first I had a bug in EXCEL ...thanks
    Last edited by gerry405; 10-09-2005 at 04:00 AM.

  5. #5
    gerry405
    Guest

    Re: macro's problem


    Dave

    Intially, I thought that your posted code was working but it turns out
    that after checking it does not seem to do anything, could you? if you
    have time suggest someother way for me to sort on a date or even better
    still, having a prompt for inputing the date that need I need to sort on
    (as this changes from day to day)

    gerry405 Wrote:
    > Dave,
    >
    >
    > thank you for your quick response, that code worked a treat...
    >
    > Alf,
    >
    > You answered another question that was bugging me, about
    > Dates/Formating...I thought at first I had a bug in EXCEL ...thanks
    >
    >
    > --
    > gerry405
    > ------------------------------------------------------------------------
    > gerry405's Profile:
    > http://www.excelforum.com/member.php...o&userid=27939
    > View this thread:
    > http://www.excelforum.com/showthread...hreadid=474279



    --
    gerry405

  6. #6
    Dave Peterson
    Guest

    Re: macro's problem

    Instead of trying this:
    Selection.AutoFilter Field:=21, Criteria1:="<="&clng(dateserial(2005,9,30))
    maybe you could use the same format as you see in the data:

    Selection.AutoFilter Field:=21, _
    Criteria1:="<="&format(dateserial(2005,9,30),"dd/mm/yyyy")


    Excel/VBA doesn't always play nice with dates.



    gerry405 wrote:
    >
    > Dave
    >
    > Intially, I thought that your posted code was working but it turns out
    > that after checking it does not seem to do anything, could you? if you
    > have time suggest someother way for me to sort on a date or even better
    > still, having a prompt for inputing the date that need I need to sort on
    > (as this changes from day to day)
    >
    > gerry405 Wrote:
    > > Dave,
    > >
    > >
    > > thank you for your quick response, that code worked a treat...
    > >
    > > Alf,
    > >
    > > You answered another question that was bugging me, about
    > > Dates/Formating...I thought at first I had a bug in EXCEL ...thanks
    > >
    > >
    > > --
    > > gerry405
    > > ------------------------------------------------------------------------
    > > gerry405's Profile:
    > > http://www.excelforum.com/member.php...o&userid=27939
    > > View this thread:
    > > http://www.excelforum.com/showthread...hreadid=474279

    >
    > --
    > gerry405


    --

    Dave Peterson

  7. #7
    gerry405
    Guest

    Re: macro's problem


    Dave,

    The code you suggested finally does the trick... thanks very much for
    that help!

    also is there a way of prompting for a date as part of the macro, that
    way the user inputs the date (in the correct format) then the macro
    filter runs on based on what ever is input


    Dave Peterson Wrote:[color=blue]
    > Instead of trying this:
    > Selection.AutoFilter Field:=21,
    > Criteria1:="="&clng(dateserial(2005,9,30))
    > maybe you could use the same format as you see in the data:
    >
    > Selection.AutoFilter Field:=21, _
    > Criteria1:="="&format(dateserial(2005,9,30),"dd/mm/yyyy")
    >
    >
    > Excel/VBA doesn't always play nice with dates.
    >
    > gerry405 wrote:
    >
    > Dave
    >
    > Intially, I thought that your posted code was working but it turns
    > out
    > that after checking it does not seem to do anything, could you? if
    > you
    > have time suggest someother way for me to sort on a date or even
    > better
    > still, having a prompt for inputing the date that need I need to sort
    > on
    > (as this changes from day to day)
    >
    >
    > Dave Peterson



    --
    gerry405

  8. #8
    Dave Peterson
    Guest

    Re: macro's problem

    Something like this??

    Option Explicit
    Sub testme01()

    Dim myDate As Variant

    myDate = InputBox(Prompt:="Enter a nicely formatted date")
    If Trim(myDate) = "" Then
    Exit Sub
    End If

    On Error Resume Next
    myDate = CDate(myDate)
    If Err.Number <> 0 Then
    MsgBox "Please enter a date"
    Exit Sub
    Err.Clear
    End If

    'just to show it worked
    MsgBox Format(myDate, "mmmm dd, yyyy")

    End Sub

    ====
    But working with dates can be confusing.

    If I type 01/02/03, how will you ever be sure that you get the date that I
    meant?

    An alternative...

    Maybe use a calendar control.
    Ron de Bruin has some tips/links at:
    http://www.rondebruin.nl/calendar.htm


    gerry405 wrote:[color=blue]
    >
    > Dave,
    >
    > The code you suggested finally does the trick... thanks very much for
    > that help!
    >
    > also is there a way of prompting for a date as part of the macro, that
    > way the user inputs the date (in the correct format) then the macro
    > filter runs on based on what ever is input
    >
    > Dave Peterson Wrote:
    > > Instead of trying this:
    > > Selection.AutoFilter Field:=21,
    > > Criteria1:="="&clng(dateserial(2005,9,30))
    > > maybe you could use the same format as you see in the data:
    > >
    > > Selection.AutoFilter Field:=21, _
    > > Criteria1:="="&format(dateserial(2005,9,30),"dd/mm/yyyy")
    > >
    > >
    > > Excel/VBA doesn't always play nice with dates.
    > >
    > > gerry405 wrote:
    > >
    > > Dave
    > >
    > > Intially, I thought that your posted code was working but it turns
    > > out
    > > that after checking it does not seem to do anything, could you? if
    > > you
    > > have time suggest someother way for me to sort on a date or even
    > > better
    > > still, having a prompt for inputing the date that need I need to sort
    > > on
    > > (as this changes from day to day)
    > >
    > >
    > > Dave Peterson

    >
    > --
    > gerry405


    --

    Dave Peterson

+ 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