+ Reply to Thread
Results 1 to 5 of 5

How to convert date format into number format (VBA)

  1. #1
    Registered User
    Join Date
    08-31-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2007
    Posts
    43

    How to convert date format into number format (VBA)

    I have a form which the user inputs a date, eg. 29/03/2012. Actually, the form automatically fills in today's date when the form is initialised.
    The script then sets a cell to this value. The problem is, it's formatting it as text, despite the cell being formatted as a date.

    How do I convert this to a number format which excel cells use. (eg. 49997)

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: How to convert date format into number format (VBA)

    If you have a textbox, label, combo that is displaying a date, to save it to a cell is just

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    The cell receiving the date must be formatted as a date. In your case "dd/mm/yyyy".
    --------------------------------------------------
    You could also force the Date type like this

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-31-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: How to convert date format into number format (VBA)

    To my knowledge, that's what I've done.

    In the cell, it says 29/03/2012. If I right click and set format, it will say it is formatted as a date. The only problem is, it's not a date, it's text. For some reason it says it's a date but it's LYING.

    I have a countif which counts all the cells in a column that are greater than 0. The column is for dates, so it's basically counting all the cells with dates/numbers > 0, but it isn't counting the cells that the script has input. Ergo it must be detecting it as text.

    As I said, if I select the cell and immediately press enter, it will properly format as a date again.

  4. #4
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: How to convert date format into number format (VBA)

    Do you need to use VBA? Have you tried =DATE(RIGHT(A1,4),MID(A1,4,2),LEFT(A1,2)), where A1 is the date you are trying to reformat?

    Try running this formula all the way down, and then copy/paste special/values back to the original column.

  5. #5
    Registered User
    Join Date
    08-31-2011
    Location
    Bristol
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: How to convert date format into number format (VBA)

    Yeah the VBA makes things much quicker.

    I found out how to do it now though. I renamed the text box on the form to "oDate" and then in the function I have called "Input", used this code:

    Please Login or Register  to view this content.
    The only trouble now is, the "AddResult" function I have doesn't like DateInput as a Long, so I'll have to write some more code to make that work.

    Problem solved though, 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