+ Reply to Thread
Results 1 to 8 of 8

check date format

  1. #1
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    check date format

    hi guys I am using the below code on textbox 1 (userform) that runs smoothly doing what it is supposed to do, what I need is some code that will check the date format in textbox1, to ensure it is entered as dd/mm/yyyy, if correct then run the afterupdate code if not then show a message asking the user to enter the date correctly, I also have some code that copies all the data from the userform to another sheet on a command button, could some code here highlight if the format is not correct before copying
    Please Login or Register  to view this content.
    scouse13

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: check date format

    Hi there,

    See if the following code does what you need:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: check date format

    Quote Originally Posted by Greg M View Post
    Hi there,

    See if the following code does what you need:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    thanks for that greg, the only other thing I need is for the focus to go back to textbox1 so the user has to change the format, at the moment, once the message box is acknowledged the user could just carry on without changing the format

    scouse13

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: check date format

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    I don't think you can use the AfterUpdate event to guarantee that the User will change an invalid value - the TextBox will receive the focus but the User can just leave the invalid value in place and tab to or select another control. The displayed (invalid) value will not have changed, so the AfterUpdate event will not be triggered.

    The following code uses the Exit event - see if it does what you need:

    Please Login or Register  to view this content.

    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M

  5. #5
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: check date format

    hi greg, thanks for your help, I figured out by adding the bit of code below at the end of the Else statement, this should force the user to tab back to enter the date correctly, if not before the, the code I have on the update command button before everything is copied checks also to ensure the textbox is not blank, this all seems to work fine.
    thanks again

    Please Login or Register  to view this content.
    scouse13

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,418

    Re: check date format

    Quote Originally Posted by Greg M View Post
    Please Login or Register  to view this content.
    In the code you posted in Message #2 and then again in Message #4, you had the above series of If..Then blocks. I am wondering why you chose to test TextBox1 three separate times rather than just once...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 05-02-2020 at 03:32 AM.

  7. #7
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: check date format

    Quote Originally Posted by Rick Rothstein View Post
    In the code you posted in Message #2 and then again in Message #4, you had the above series of If..Then blocks. I am wondering why you chose to test TextBox1 three separate times rather than just once...
    Please Login or Register  to view this content.
    Good point Rick, That's why you are a valued forum contributor, and I use this site a lot for help, from people like yourself, didn't really think about it to be honest- but it being pointed out to me, will hopefully make me think in future, thanks a lot for the advice
    scouse13

  8. #8
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: check date format

    @Scouse13

    Hi again,

    Although your approach "works" it probably isn't the most elegant from either a User or a programming point of view

    Clearing the input box means that the User is forced to re-enter the full 10-digit date value, but in many cases all that will be required is to insert a missing zero or a four- (instead of two-) digit year value.

    Clearing the input box also means that the AfterUpdate event is triggered again, and although that doesn't really matter much in this case, the routine is needlessly re-executed and you're starting down the road of recursive programming which is a whole new ballgame


    @Rick Rothstein

    Hi Rick,

    Well spotted!

    Without thinking too much about it I just grabbed the OP's code and stuffed it inside a date format test - just goes to show that an extra pair of eyes is always useful!


    Regards to you both,

    Greg M

+ 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] Check Date Format Input into Excel Column with VBA code ("DD/MM/YYYY" format)
    By inoka in forum Excel Programming / VBA / Macros
    Replies: 44
    Last Post: 04-17-2018, 05:26 AM
  2. [SOLVED] Check Date Format Input into Excel Column with VBA code ("DD/MM/YYYY" format)
    By inoka in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 12-14-2017, 08:30 AM
  3. check for date in mixed text/date and format cell.. Help!
    By berbchid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-24-2013, 03:42 AM
  4. Check Date format before proceeding
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2013, 12:39 AM
  5. [SOLVED] Check IF a cell is a certain DATE format?
    By biddum in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-31-2012, 04:18 AM
  6. Check system date format
    By Snoopy2003 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-16-2011, 05:40 PM
  7. How do I check if a variable is in the date format?
    By thedryden in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2006, 01:50 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