+ Reply to Thread
Results 1 to 10 of 10

Converting a String to a Numeric Date

  1. #1
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Converting a String to a Numeric Date

    I need to be able to locate a date in a spreadsheet bassed uppon a user input date. I am looking for the week that contains the user's date. However it seems even if I initialize the value as Date, it still reads the user's input as a string.

    Is there an easy way make sure the system automaticaly recognizes the user's input as a date, or is there a way to quicly convert the string to a date?

    Thanks!
    Last edited by Pherion; 07-13-2009 at 02:27 PM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Converting a String to a Numeric Date

    How is the date being entered? In a cell, input box, user form? Though you mention"initialize", we don't want to make assumption about the data entry method.

    You might consider using the pop up calendar control for users to select the date. This would help insure a valid date is always entered.

  3. #3
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Converting a String to a Numeric Date

    Its a user form, just an input box. I used this line in the Initalize function:
    Please Login or Register  to view this content.
    Perhaps this isn't doing what I thought it would? That is create the variable as a "Date" type?

  4. #4
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Converting a String to a Numeric Date

    Create a new user form and put the calendar control object in it. You may need to rifght click the control toolbox and choose "additional controls" to find and add the Calendar Control.

    Drag and size the calendar control onto the new form.
    Double click the form and modify this code.
    (I assume you are entering the date into a text box. If so, set the text box entry even to show the calendar form.)
    For the activate event:
    Please Login or Register  to view this content.
    Calendar Click Event
    Please Login or Register  to view this content.
    If entering the date into a cell, the modify this:
    Please Login or Register  to view this content.
    Use this to get the date into the worksheet:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Converting a String to a Numeric Date

    Trying a few differnt things now. The only calander control item I can locate in my version is for MS Office Project, and it doesn't behave the same as what you are describing above.

    So I tried to use the UserForm_Activate() sub to initalize my already existing entry box value as a date, and that worked.

    However, I keep getting type mismatch errors when I attempt to compare the date to a date in the spreadsheet. All I am doing is trying to compare to see if the user's entered date is larger than the begining of each week, in an attempt to locate which week I need to input data. I would assume that they need to be in numeric format to do this.

    Does VB not treat a "Date" similarly to excel, and automatlcy treat it as a number when you execute a comparison?
    Can you offer any advice on this?

  6. #6
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Converting a String to a Numeric Date

    Wow... I can tell its been a while since I wrote code this much....

    I had a variable declared as CompDate, and then later when I tested it I used DateComp

    <*blush*>

    Let me see if this fixes the problem....

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Converting a String to a Numeric Date

    I'm probably misunderstanding what you're doing, but ...
    Please Login or Register  to view this content.
    The method would be equally applicable if the string source were a textbox instead of InputBox.
    Last edited by shg; 07-13-2009 at 02:29 PM.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Converting a String to a Numeric Date

    CDate() is exactly what I needed. Seems to be working now. Thanks!

  9. #9
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Converting a String to a Numeric Date

    Invalid date entries is common. I was only attempting to point you to a solution that provides a method of entering a valid date, but should have just provided the code given by shg. Sorry if overwhelmed you.

  10. #10
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Converting a String to a Numeric Date

    No problem Palmetto, your advise was helpfull as well. Thanks!

+ 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