Solved

Excel: only allow user to enter a date

Posted on 2010-09-22
5
885 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:teylyn
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:teylyn
teylyn 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

22 Experts available now in Live!

Get 1:1 Help Now