+ Reply to Thread
Results 1 to 16 of 16

combining date and time

  1. #1
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    combining date and time

    If i use the following:

    CDate(Now()) then i get 4/28/2011 12:38.....

    How can I get the same format when cell A1 has the date and cell A2 has the time?

    I have tried combining the date and time but to no avail.

    Thanks,

    JJ
    Last edited by h_aesa1; 05-05-2011 at 05:36 PM.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: combining date and time

    If I enter 4/28/2011 in A1 and 12:38:00 PM in B1 and the formula =A1+B1 in cell C1, I get 4/28/2011 12:38
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: combining date and time

    Hello h_aesa1,

    In VBA the code would be...
    Please Login or Register  to view this content.

    The date in VBA is represented as a decimal fraction. The date is the integer portion (left side of the deicmal point) while the time (seconds since midnight) is the decimal portion (right side) of the decimal point. Adding the 2 cells returns a decimal fraction that will be converted in the Short Date format for your system.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: combining date and time

    thank you for the reponses.. had a quick question

    Say i was to use a userform to input to a sheet

    so

    ComboBox1 = Date
    ComboBox2 = Time
    ComboBox3 = AM or PM

    I believe I can do the Sheet1.Range("A1").value = CDate(ComboBox1) + (TIME) but how do I combine the ComboBox2 with the ComboBox3 to get the full time display

    Thanks,

    JJ

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: combining date and time

    Hello JJ,

    You can combine all the text together before you convert the string and save it in Range("A1").
    Please Login or Register  to view this content.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: combining date and time

    It depends on the international settings of your system:

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    But i'd prefer a datepickercontrol to comboboxes to select a date & Time
    Please Login or Register  to view this content.



  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: combining date and time

    Hello snb,

    CDate is internationally aware.

  8. #8
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: combining date and time

    I am getting a run time error, run time error '13' Type Mismatch,

    Here is the code I am using:

    HTML Code: 
    cbxDate = combo box where they can select the date
    cbxTime = combo box where times are listed
    cbxAMPM = choose AM or PM

    Not sure if I have a typo but I copied the exact code from Leith, just different names

    Thanks,

    JJ

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: combining date and time

    Hello JJ,

    It would help to see your workbook. Can you post a copy of it?

  10. #10
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: combining date and time

    Had another question....

    I was using an older Calendar Control... so it was only using the calendar... but I was able to look into the DatePicker control and have that as well.

    I think this might be a better option but... Is there anyway where you can choose the date in one control and then choose the time in another control.

    What I am trying to accomplish is for someone be able to choose a date and then choose a time and format both choices to the following

    4/28/2011 13:30

    and then put in say Sheet1.Range("A1")


    thanks,

    JJ

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: combining date and time

    Open the attachment (enable macros).
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: combining date and time

    thanks SNB... i like the setup but what would be code to take the results and put into A1 in the following format. 5/3/2011 14:30

    So if they choose 5/3 for the date and then choose 2:30 pm as the time... i want them to be able to hit a command button (which i can do) and put into ("A1").... 5/3/2011 14:30


    Currently I use

    HTML Code: 
    and it puts it into the above format... i just want them to be able to choose and accomplish the same thing.

    Thanks,

    JJ

  13. #13
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: combining date and time

    to further expand on previous post.

    I went into your form and put a command button that shows a msgbox for the value of DTPicker1 and DTPicker2

    I chose the Date of 5/3/2011 and the time of 9:30 am

    then

    Msgbox 1 shows = 5/3/2011
    Msgbox 2 shows = 12/17/2007 9:30:00 AM

    So obviously this is not going to work when trying to combine them... is there anyway to send the Date of DTPicker1 to the value of DTPicker2 so it would reflect... 5/3/2011 9:30:00 AM


    JJ

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: combining date and time

    Have a look at the atttachment.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    02-09-2009
    Location
    Washington, US
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: combining date and time

    that is Awesome... thank you so much!!!!

    Take Care,

    JJ

  16. #16
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: combining date and time

    I will.

    to download the DTPicker look for MSComCtl2.ocx

+ 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