+ Reply to Thread
Results 1 to 26 of 26

Date Format changes from Userform to Spreadsheet

  1. #1
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Date Format changes from Userform to Spreadsheet

    Hi All,

    Attached is a test code example of the problem I am encountering. I have looked at the numerous posts with similar if not the same problem however none that i have tried including CDate has managed to correct the problem.

    As with other posts I need to format the date in a dd/mm/yyyy format. When the date is selected it appears correct in the userform however when it is added to the spreadsheet it changes to US format mm/dd/yyyy. My computer's regional settings are set correctly. I am also using Excel 2010.

    When the data is returned from the spreadsheet to be edited it is formatted correctly in the userform.

    Strange is how the data appears on the spreadsheet. If the day is 13 or more e.g. 17/04/2014 it will be formatted correctly on the spreasheet but be formatted as text. Conversely, if the day is 12 or below e.g. 12/04/2014 (April 12) will appear as 04/12/2014 (December 4) and be formatted as a date.

    I have checked the formatting of column B where the dates are contained and if I change the formatting from Date (Short Date) to General. The dates with a day of 12 or below (second example above) will change to a number e.g.4/6/1999 will become 36315 whereas the first examples remain as text.

    I need the dates to be "dates" as I am calculating a current DOB.


    I have tried to use DatePicker however this has not worked either for different reasons in that i am unable to capture a NULL error when reseting the userform controls and workign with a blank textbox in conjunction with the calculating the age.

    In the attached example I am using Monthview. I have tried RoyUK's CalendarForm and this resulted in the same formatting error as described above.

    I apologise if there is a solution i have overlooked and appreciate any assistance.

    Thanks
    Darren
    Attached Files Attached Files
    Last edited by Sorjas; 09-12-2014 at 01:51 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Date Format changes from Userform to Spreadsheet

    Hi Darren,

    Try the following macro which seemed to work for me. The only actual change is in red. The other changes were made so it would be easier for me to read the code.

    Lewis

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Thank you Lewis. I must have coded the CDate incorrectly in my previous attempts. It looks obvious probably because it is!

    Incorporating the test example into my project worked well. I also managed to solve a "lookup function" problem in my project using the same logic by using CDate correctly and changing from .value to .text for the date fields.

    Even with the correct code you provided on lookup they reverted to US format in the userform (wasnt happening before) but when edited were being saved correctly to the spreadsheet. The problem appears to be solved. Thank you once again!

    Cheers
    Darren

  4. #4
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Lewis,

    I have encountered a type mismatch error 13 on this line of code when the cell is blank
    Please Login or Register  to view this content.
    and on when cycling through I get a 12:00:00 AM cell value.

    I assume both have to do with the fact there is an empty value and the date cannot be null?

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Date Format changes from Userform to Spreadsheet

    Hi Darren,

    Welcome to the world of Defensive Programming, trying to anticipate what will happen when nothing can go wrong, go wrong, go wrong. Notice in the 'Add Branch' that the addition of the 'Id' is moved to AFTER the test for the date. If the date is NOT VALID, then nothing will be added.

    Try the attached updated macro (changes in red):
    Please Login or Register  to view this content.
    Lewis

  6. #6
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Lewis,

    Sorry for the late response. I have been trying the code in my excel database project with no success. I believe the existing code i have written is problematic. Unfortunately I am self-teaching and not fluent enough in VBA to merge the code above correctly.

    I have stripped the database back to the basics Example Database-1. The main problem area with regards to the dates are the Add and Edit buttons and the lookup list which is double-clicked below the lookup button.

    The original example code in this post was to see how the cDate worked and if i could then incorporate into the project. I did not want to overwhelm those kind enough to assist however this last bit has me stumped so I am including the file.

    If there is a better way to do the dates I would be happy to try it. The only calculation with the date fields is the DOB to get the Age. The entry date and application date are not used for calculation.

    I would appreciate your insight into whether your code can be implemented or if the existing code needs a rewrite? Of course if others would like to chime in you are also welcome as well

    Thanks
    Darren
    Attached Files Attached Files

  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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    I recently finished a International Date Picker UserForm. This will correctly display dates based on your regional settings. You can also chosse to copy the date to a fixed cell on the worksheet or to the active cell. The date formats can be according to what the system has available. Try it out and let me know if you can use it.
    Attached Files Attached Files
    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!)

  8. #8
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Leith,

    I ran the code and received the following error message: Runtime error '1004': Programmatic access to Visual Basic Project is not trusted.

    The debugger stopped on
    Please Login or Register  to view this content.

  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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    Under File on the Ribbon...
    1. Click Options
    2. Click Trust Center
    3. Click the button Trust Center Options...
    4. Click Macro Settings
    5. Click the check box Trust access to the VBA project object model. The box should show a check. If not, click it again.

  10. #10
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Leith,

    Thanks for the menu instruction. The code runs however the dates when changed to 01/01/2014 format become US format. I have tried using fixed cell and active cell as well as formatting the sheets as custom or as Date>Short Date and the the date remains in US format.

  11. #11
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Date Format changes from Userform to Spreadsheet

    Leith,

    Nice job on the Calendar Form as always, and thank you for sharing. Since I am using Excel 2003 and Vista, I had to make a few changes to make it run:
    a. Added 32 bit CONDITIONAL COMPILATION in Module 'API_Timer'.
    b. GetShortTime() in Module 'CalendarAPI' gets the time format using registry item 'sShortTime' which in Vista (and probably XP) the item name is 'sTimeFormat'. I modified the code as follows:
    Please Login or Register  to view this content.
    c. I added Module 'ModLJM' which contains function LjmGetOperatingSystemVersion(). You probably have to add CONDITIONAL COMPILATION to get it to compile in 64 bit Excel. I was unable to find a 64 bit reference to the 64 bit code for the GetVersionExA() Api and it's user deifned data structure:
    Please Login or Register  to view this content.

    Additionally, I got a runtime error in the code, after changing the date cell location. It seems that when changing the date cell location the Calendar Form goes from modal to non-modal. After the location is changed, the Calendar Form appears to still be non-modal. It the 'Show Calendar' Command Button is selected, the run time error occurs (Error 400 = Form already displayed - Can't show modally).

    Lewis
    Attached Files Attached Files
    Last edited by LJMetzger; 09-17-2014 at 10:37 AM.

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Date Format changes from Userform to Spreadsheet

    FYI you only need to add PtrSafe for 64 bit compatibility of GetVersionEx.
    Remember what the dormouse said
    Feed your head

  13. #13
    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: Date Format changes from Userform to Spreadsheet

    Hello Lewis,

    Thank you for taking the time to add the code for the different Office versions. I was aware of the registry problem but did not know the key name or names for 2007 and Vista. Thanks for updating that piece also. Looks like I need to release a version 4 of this.

    Any feedback from users is welcome.

  14. #14
    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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    I don't know what to tell you. The API code in the workbook I posted, taps into the regional and language settings of your system.

    Recheck your Region and Language settings. If the problem is not there then it could be a problem with tying into the code I wrote. If that is the case, you should post the most current copy of your workbook so I can review it.

  15. #15
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Date Format changes from Userform to Spreadsheet

    Hi Darren,

    Your existing code is fine, you just have to understand it. It is written to be efficient, not to be understood and maintained. For example all the TextBoxes in the UserForm have a generic name of 'Reg1' through 'Reg13'.

    When the data is saved, it looks like in Sub 'cmdAdd_Click()':
    Please Login or Register  to view this content.
    If it were my project, I would have named the textboxes to be more descriptive, e.g. 'TextBoxFirstName', 'TextBoxDateOfBirth', etc. This is just my programming style. Each person and/or organization has their own style (and/or standards). When I have to look at the code in the future, I won't have any idea what a 'Reg5' is, but 'Date Of Birth' will have meaning to me.

    I would change the code as follows:
    Please Login or Register  to view this content.
    I would move the code that adds the data to an ordinary code module such as 'Module1':
    Please Login or Register  to view this content.
    I prefer the Ordinary Code Module because I think:
    a. Ordinary Code modules are easier to debug than UserForm modules.
    b. The routines can be tested semi-independently of the UserForm (if during debugging the UserForm is Non-Modal [i.e. UserForm1.Show False]). Non-Modal UserForms allow access to the spreadsheet while the UserForm is active. Data Can be put in the UserForm, and then in the debugger (or from the Spreadsheet by calling the Macro or from a Temporary Command Button) the routine can be tested.

    My code is a lot longer, runs slower (but in this situation run time is NOT an issue), but it is a lot easier for me to understand and debug.

    I hope this helps.

    Lewis
    Last edited by LJMetzger; 09-18-2014 at 06:01 AM. Reason: ActiveSheet.nextrow changed to nextrow per Rory - TY Rory.

  16. #16
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Thanks Lewis. I will work through the last code and let you know how i go.

  17. #17
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Leith,

    Regional Settings are correct. However I did discover the following which may be of some use. I run the code and as mentioned before the date selected is added to the spreadsheet in US Format. If i then select the whole worksheet and change the cell format to Custom > dd/mm/yyyy it changes to the correct International Date format. Importantly as soon as I press the Calendar command button the date format changes the dates on the spreadsheet back to US format. Is this significant?

  18. #18
    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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    As I mentioned in the last post, if the regional settings are correct then you should post a copy of the most recent workbook for review.

  19. #19
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Leith,

    Apologies I overlooked your instruction. Here is the Calendar file mentioned above.
    Attached Files Attached Files

  20. #20
    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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    Okay, the UserForm works correctly. The problem is with the date format listing. Since both the day and month are 1, you can not tell which is which when they are switched. So, I am working on correcting that.

  21. #21
    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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    Try this version of the workbook. I have added a legend to the "Date" tab that shows the day, month, and year used in the date format. This should fix the problem.
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Lewis,

    I have modified the Add button with your code and made the necessary additions to include the other controls. When I press the Add button a Run-Time error '438': Object Doesn't support this property or method occurs on the line of code in bold within the AddDataToWorksheet module. I have included the modified database project file Example Database-2.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  23. #23
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Hi Leith,

    i downloaded the file Userform ver 3a and it appears to function the same. The legend does not appear on the Date tab??

  24. #24
    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: Date Format changes from Userform to Spreadsheet

    Hello Sorjas,

    I have uploaded the file again. This time it should be correct. Not sure what the problem was with the last upload. The file on my computer was correct, yet the file that was upload was not. It must have been PFM.

    Check this workbook out and let me know the results.
    Attached Files Attached Files

  25. #25
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Date Format changes from Userform to Spreadsheet

    Rather than
    Please Login or Register  to view this content.
    each line should start with just
    Please Login or Register  to view this content.
    since nextrow is a Range object.

  26. #26
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Duplicate post removed.
    Last edited by Sorjas; 09-29-2014 at 11:28 PM. Reason: Duplicate Post

  27. #27
    Registered User
    Join Date
    04-23-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Date Format changes from Userform to Spreadsheet

    Sorry for the late response. I have been rewriting the code using Lewis' example. Thank you Rory for the correction above and Lewis for the advice on making the code readable even if it's longer

    The dates are working as intended. Happy with the progress. So thank you for the code and advice.

    Leith I tried Calendar version3b example and it was coming up with the same issue - not displaying in international date format. I do not know why. I am unable to attach the file at present something wrong with the php code on the Go Advanced button - will do as soon as I can. Thank you for the code and hopefully the date issue can be nutted out. It is a great calendar
    Attached Files Attached Files

+ 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. [SOLVED] Userform date entered in UK format but showing in US Format in spreadsheet
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-23-2013, 01:59 PM
  2. Date format on UserForm changes on Spreadsheet
    By darthelvis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2013, 05:41 PM
  3. [SOLVED] Userform displays in (MM/DD/YYY) format, when spreadsheet stores as (DD/MM/YYYY) format
    By david1987 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2012, 10:39 AM
  4. Use spreadsheet cell format to set userform text box format - how?
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-08-2010, 07:17 AM
  5. Copying date values from Userform to Spreadsheet keeping same format
    By mhuddles1981 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-12-2010, 01:53 AM

Tags for this Thread

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