Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Strings into dates (WinNT4, XL97)

    This is stupid, I'm sure I've done it before, but can I remember or find it now?! Can I heck ...

    How do I get VBA to read a string such as "Jun 05" as the date, June 2005? I don't mind adding a day to it (ie 1 June 2005) if that's necessary ...

    Many thanks and sorry for being so stupid today!
    Beryl M


  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Strings into dates (WinNT4, XL97)

    Hi Beryl,
    Try <code>CDate("01 " & strDate)</code> where <code>strDate</code> is your string to convert.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Strings into dates (WinNT4, XL97)

    Brilliant! I'm sure that's not how I did it before (I've never heard of CDate before!) but this is much better!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Beryl M


  4. #4
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Strings into dates (WinNT4, XL97)

    See also the VBA Function 'IsDate' - it's handy to check the validity of the date string being passed:

    If IsDate("01 " & strDate) Then
    mydate = CDate("01 " & strDate)
    Else
    Call PanicMode ' or the errorhandler of your choice
    End If
    -John ... I float in liquid gardens
    UTC -7ąDS

  5. #5
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Strings into dates (WinNT4, XL97)

    If it's just "June 2005" you want, then you could add a Format string to Rory's solution:

    <font face="Georgia">Format(CDate("01 " & strDate), "MMMM yyyy")</font face=georgia>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Strings into dates (WinNT4, XL97)

    Be aware that CDate will result in a true date value, while Format will result in a text string that looks like a date.

  7. #7
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Strings into dates (WinNT4, XL97)

    I know, Hans. Thats why I started with: "If it's just..." Maybe I should've chosen "only" instead.

Posting Permissions

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