Solved

Excel: only allow user to enter a date

Posted on 2010-09-22
5
886 Views
Last Modified: 2012-05-10
What VBA would I need so that a user can only enter a date in my textbox in my Excel spreadsheet. (i.e. if the user enters "Hello", the textbox won't allow it, maybe it will delete "Hello" and display a messagebox saying: "Dates only please" etc.)
0
Comment
Question by:ouestque
5 Comments
 

Accepted Solution

by:
priteshjchauhan earned 167 total points
ID: 33740270
   With Selection.Validation
        .Delete
        .Add Type:=xlValidateDate, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="1/1/1900", Formula2:="=NOW("
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "Error"
        .InputMessage = ""
        .ErrorMessage = "Please enter a valid date."
        .ShowInput = True
        .ShowError = True
    End With
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 33740430
Hello ouestque,

do you mean a textbox or a cell? A date is a number, so data validation code for a cell only needs to check if the number is greater than 0, like the code below. If you want to limit data entry of future dates, then the previous code will work, if you add a closing parens in the NOW() parameter.

You can also specify that the maximum date is, for example 30 days from today. For this, use

Formula2:="=NOW()+30"

If you want to validate a textbox, the code will need to be completely different. Please clarify.

cheers, teylyn
With Selection.Validation
        .Delete
        .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
        Operator:=xlGreater, Formula1:="1"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "Wrong data type"
        .InputMessage = ""
        .ErrorMessage = "Enter valid dates only"
        .ShowInput = True
        .ShowError = True
    End With

Open in new window

0
 
LVL 50

Assisted Solution

by:Ingeborg Hawighorst
Ingeborg Hawighorst earned 166 total points
ID: 33740480
This code will check a textbox and allow only dates.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Shapes.Range(Array("TextBox 1")).Select
    On Error GoTo Error01
    vvar = DateValue(Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text)
    Exit Sub
Error01:
    Application.EnableEvents = False
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = ""
    Application.EnableEvents = True
    MsgBox "Please enter a valid date"
End Sub

Open in new window

0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 167 total points
ID: 33741863
Assuming a userform, then something like this (note though that IsDate is quite expansive in its determination of dates):



Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    If Len(TextBox1.Text) > 0 Then

        If Not IsDate(TextBox1.Text) Then

            MsgBox "must enter a date"

            Cancel = True

        End If

    End If

End Sub

Open in new window

0
 

Author Closing Comment

by:ouestque
ID: 33742123
Awesome answers thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now