+ Reply to Thread
Results 1 to 9 of 9

Restrict the date format entered in a cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Restrict the date format entered in a cell

    Hi,

    I am working on a excel sheet where the user has to enter a date.

    Now when the user enters the date in a format than the pre-determined format(say, 03/10.2010), an error message has to pop up.

    fpr ex- my predetermined format is mm/dd/yyyy. If the user enters dd-mmm-yyyy then it has to give an error message "wrong format....."

    I know MACROS have to be used to get this functionality. But iam a novice in macros...

    Any help is appreciated.

    Thanks

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

    Re: Restrict the date format entered in a cell

    Hello skonduru,

    A few questions....
    1. Is this validation to apply to all cells on the worksheet or just a select range of cells?
    2. Which version of Excel are you using?
    3. Which country is this version of Excel for?

    You should update your user profile to include this information.
    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!)

  3. #3
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Smile Re: Restrict the date format entered in a cell

    hi ross....

    1) the validation applies to a selected range of cells in a colomn.

    2) version of excel- 2003.

    3) Country is US......

    will update my profile today

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Restrict the date format entered in a cell

    Maybe like this. Code goes in the Sheet module.
    Option Explicit
    
    Const sRng          As String = "A1:A10"        ' change as desired
    Const sFmt          As String = "mm/dd/yyyy"    ' change as desired
    Const sLike         As String = "##/##/####"    ' change to agree with sFmt
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim sDate        As String
    
        With Target
            If .Count > 1 Then Exit Sub
    
            If Not Intersect(.Cells, Me.Range(sRng)) Is Nothing Then
                sDate = .Text
                .NumberFormat = "@"
                Application.EnableEvents = False
                .Value = sDate
                Application.EnableEvents = True
            End If
        End With
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        With Intersect(Target.Cells, Me.Range(sRng))
            On Error Resume Next
            Application.EnableEvents = False
            
            If .Count > 1 Then
                Application.Undo
                MsgBox "One cell at a time, please."
            
            ElseIf IsDate(.Text) And .Text Like sLike Then
                .NumberFormat = sFmt
                .Value = CDate(.Text)
            
            Else
                MsgBox "Wrong format!"
                Application.Undo
            End If
        End With
            
        Application.EnableEvents = True
    End Sub
    There's nothing you can do to know that 01/06/2010 was really intended to mean 1 Jun 2010.
    Last edited by shg; 04-22-2010 at 11:34 AM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Unhappy Re: Restrict the date format entered in a cell

    Now, how do i integrate this code to my excel sheet?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Restrict the date format entered in a cell

    Put the code in the appropriate Sheet module.

    Adding Code to a Sheet module
    1. Copy the code from the post
    2. Right-click on the tab for the relevant sheet and select View Code. This opens the Visual Basic Editor (VBE) and shows the object module for the selected worksheet.
    3. Paste the code in the window
    4. Press Alt+Q to close the VBE and return to Excel

  7. #7
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Re: Restrict the date format entered in a cell

    bump......................

+ 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