+ Reply to Thread
Results 1 to 8 of 8

Handling dates in VBA Question.

  1. #1
    Registered User
    Join Date
    11-07-2008
    Location
    Planet Earth
    Posts
    8

    Handling dates in VBA Question.

    The value in the cell is 2008-11-29

    Why then does this code return a false?

    Please Login or Register  to view this content.
    If I switch the cell reference to

    Please Login or Register  to view this content.
    It returns a True.

    Is there a certian date format I can use that will be recognized as such by both the worksheet, and the VBA macro?

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi,

    Because in the first case the cell is NOT greater than 29 Nov 08. In the second case the cell IS greater than 21 Nov 08.

    Why do you expect something different?

    i.e. it's performing exactly as it should.

    Do you perhaps want to change the > to a >=

    Rgds
    Last edited by Richard Buttrey; 11-27-2008 at 01:26 PM. Reason: Clarification

  3. #3
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Date handling

    Hi

    The mid function you are using returns a string so in itself it is difficult to make any comparisons with dates.

    I am not sure how to do it completely in VBA but you could use the following code to make a valid comparison, using cells A1 B1 and C1.

    Please Login or Register  to view this content.
    Regards

    Jeff

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi Jeff,

    That's what I originally thought too. But entering the string "21Nov08" in a cell does in fact result in a valid excel date number. Excel is clearly very good at interpreting what one is trying to do!

    Rgds

  5. #5
    Registered User
    Join Date
    11-07-2008
    Location
    Planet Earth
    Posts
    8
    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Because in the first case the cell is NOT greater than 29 Nov 08. In the second case the cell IS greater than 21 Nov 08.

    Why do you expect something different?

    i.e. it's performing exactly as it should.

    Do you perhaps want to change the > to a >=

    Rgds

    I am not sure what you are saying. If the value in the cell is 2008-11-29, then that should be greater than 21Nov08.

    In the second case, yes it should return a true. I mentioned that, because it seems to have no odd stuff happening when you aren't comaparing stuff from the worksheet.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Try
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    11-07-2008
    Location
    Planet Earth
    Posts
    8
    Quote Originally Posted by shg View Post
    Try
    Please Login or Register  to view this content.

    Thanks, that works, though I had to change the naming style of the pictures from "21Nov08" to "2008-11-21", but that is fine.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Please mark the thread solved & rate the answers
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ 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