+ Reply to Thread
Results 1 to 9 of 9

Dates and formatting TextBoxes in a Userform

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Dates and formatting TextBoxes in a Userform

    I enter todays date into a sheet via a userform as follows:-

    Please Login or Register  to view this content.
    I have also formatted A1 as a date with the same format

    However when I read back the value it always shows in the textbox as mm/dd/yyyy.

    How can I stop this?
    John Southern

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Dates and formatting TextBoxes in a Userform

    Try:

    Please Login or Register  to view this content.
    BSB

  3. #3
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: Dates and formatting TextBoxes in a Userform

    you should be careful when using this format within VBA, VBA will first try and convert a date into american standard format, if you dont explicitly convert the dates yourself between mm/dd/yyyy and dd/mm/yyyy your going to find that your data is going to get corrupted.

    always set the cell to a text format.... this way you know excel has made any changes to it, before you use it in VBA convert it into american format first, then do whatever you need to do and then convert it back to UK standard format (dd/mm/yyyy) before you display it again.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Dates and formatting TextBoxes in a Userform

    John

    How are you putting the date in the textbox?
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Dates and formatting TextBoxes in a Userform

    Norie,
    I am using the system date I Think. As In :-
    Please Login or Register  to view this content.
    gbeats
    I am not doing any date calculations. I use the same form for entering in new data and editing old data, and I just want the date to stay stable in a UK type format.
    I don't quite follow your suggestions. Are you saying format the excel cells as text rather than dates?

    John

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Dates and formatting TextBoxes in a Userform

    John

    Where and when is the date appearing with the wrong format?

    Using Format when populating the textbox should work but when putting the value from the textbox in a cell you should be converting it to a 'real' date using DateValue or CDate and setting the format of the cell to what you require.

  7. #7
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: Dates and formatting TextBoxes in a Userform

    just be careful with using Format(Date,"dd/mm/yyyy"), if you put this result directly into a cell its likely going to mix up the dd/mm part depending on the date you use. norie's suggestion is good but you must convert it into the correct type first.....

    either enter into the sheet as a mm/dd/yyyy format first and let the formatting handle the conversion into dd/mm/yyyy

    or you can convert it into the format excel uses internally with CDbl(CDate("10/10/2016 10:30PM")) this will give you a number that excel will always understand as the correct date you just need to add a cell format to format it how you want it, but even this method is not reliable..

    just to show you how weird this gets run this code

    Please Login or Register  to view this content.

    this is what i got
    42653.9375
    42668.9375
    42668.9375
    10/10/2016 10:30:00 PM
    10/25/2016 10:30:00 PM <- as you can see CDate changes the UK format date into US format
    10/25/2016 10:30:00 PM

    since your not doing calculations on the date just set the cells to text and just assume whatever text is in the textbox is the right format....
    Last edited by gbeats101; 04-15-2016 at 02:15 PM.

  8. #8
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Dates and formatting TextBoxes in a Userform

    Norie and gbeats101, badlyspelledbouy
    Thank you all for your help on dates. I have always had difficulty with this aspect of Excel, and I think I shall be much more secure in future.

    John

  9. #9
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: Dates and formatting TextBoxes in a Userform

    Just to add, to this for anyone looking into this problem.......

    The particular problem i point out here happens when your trying to mix 2 different date styles/formats within a single localization

    obviously the localization in the UK for example will always format the date (In Excel) in the UK format and assume you mean this format, my example above is using the US localization so it assumes US standards.......

    i havent tested this with VBA internally, im not sure right now if it uses localization, but if your using excel between 2 locations using their different localization settings the dates in excel normally would be fine to work with, the problem may come with sharing a file using VBA handling dates and especially accepting date inputs from a user (is it the UK user or the US user entering this date?)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Userform Textboxes and dates
    By AndyRoo119 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2016, 03:07 PM
  2. Chronological Dates In TextBoxes On UserForm
    By yoshi_5 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-29-2015, 09:35 AM
  3. Help Populating Textboxes in Userform, With Multipage Userform.
    By mdmorgan24 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2012, 05:29 PM
  4. Formatting Multiple Userform Textboxes
    By badeye in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-01-2010, 09:14 AM
  5. Userform Help - Textboxes & offset
    By kbenstead in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 03-18-2009, 08:54 AM
  6. Userform TextBoxes
    By tqm1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-29-2007, 09:04 AM
  7. UserForm TextBoxes
    By Rob in forum Excel General
    Replies: 2
    Last Post: 08-05-2005, 10:07 PM

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