Solved

vba date validation

Posted on 2010-09-16
9
544 Views
Last Modified: 2012-05-10
I want to validate in vba that an inputbox entry is between two dates: two yrs ago and 10 yrs hence..  If true, continue; if false msg box and try again.
0
Comment
Question by:1r3o2n8
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:ymiossec
ID: 33696857
you don't need vba for that :

you just have to use data-validation on that cell, with a personalized formula as :
(for checking value in I9 between
=ET(I9>J9;I9<J10)

with J9 = 2 years ago
and J10= 10 years hence

you can personalize the message that will be displayed to the user.
He cannot validate a wrong value in the field.
0
 
LVL 2

Expert Comment

by:ymiossec
ID: 33696873
Sorry, French version of Excel ...

in english :
=AND(I9>J9;I9<J10)
0
 
LVL 81

Expert Comment

by:byundt
ID: 33697184
ymiossec uses semicolons as a list separator. In American Excel, you would use a comma.
=AND(I9>=J9,I9<=J10)                                Formula changed so allowable dates would also include the end points
0
 

Author Comment

by:1r3o2n8
ID: 33697385
I need vba because: 1) my entries are made on a userform, not spreadsheet; 2) the date extremes are based on today's date (dynamic), not a hard date..  I also like the msgbox to indicate out of range date.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 81

Expert Comment

by:byundt
ID: 33697629
The code in the snippet assumes you enter the date as a string in a textbox named TextBox1 on a userform. The date is tested when the user hits the OK button.
Private Sub cbCancel_Click()

Me.Hide

Unload Me

End Sub



Private Sub cbOK_Click()

Dim dat As Date

If IsDate(TextBox1.Value) Then

    dat = CDate(TextBox1.Value)

    Select Case dat

    Case Is < DateSerial(Year(Date) - 2, Month(Date), Day(Date))

        MsgBox "Date is too soon. Try again."

    Case Is > DateSerial(Year(Date) + 10, Month(Date), Day(Date))

        MsgBox "Date is too far in future. Try again."

    Case Else

        'Good date. Do stuff

        MsgBox "A good date was entered: " & Format(dat, "mmmm d, yyyy")

    End Select

End If

End Sub

Open in new window

ValidateDateQ26479360.xlsm
0
 
LVL 2

Expert Comment

by:insaneCFC
ID: 33699368
Try this:
Sub InputBoxCode()
Dim d  As Date


d = InputBox("Enter Date")
If d < DateSerial(Year(Date) - 2, Month(Date), Day(Date)) Or d > DateSerial(Year(Date) + 10, Month(Date), Day(Date)) Then
MsgBox "Date is out of specified time frame!"
Exit Sub
Else
    'Rest of Code
End If

End Sub

Open in new window

0
 

Author Comment

by:1r3o2n8
ID: 33703484
Attched is "wire" workbook.  I believe your code should be in Calendar1 so when date is selected your code will run.  I've tried both of your code ideas, but neither work.  Thanks for your patience.
WIRE-2010.xlsm
0
 
LVL 81

Expert Comment

by:byundt
ID: 33703693
You don't have a CommandButton3 in your userform. When I added one, the macro tested the date as expected.

I tested the code as shown below. Note that I deleted some extra End Sub statements and commented out the original code for CommandButton3.

Brad
Private Sub CommandButton3_Click()

'Range("H1").Select

    'Range(Selection, Selection.End(xlDown)).Select

    'Range(Selection, Selection.End(xlToLeft)).Select

    'ActiveSheet.PageSetup.PrintArea = Selection.Address

    'ActiveWindow.SelectedSheets.PrintPreview





'Private Sub TextBox1_Change()

'Private Sub cbCancel_Click()

'Me.Hide

'Unload Me

'End Sub



'Private Sub cbOK_Click()

Dim dat As Date

If IsDate(TextBox1.Value) Then

    dat = CDate(TextBox1.Value)

    Select Case dat

    Case Is < DateSerial(Year(Date) - 2, Month(Date), Day(Date))

        MsgBox "Date is too soon. Try again."

    Case Is > DateSerial(Year(Date) + 10, Month(Date), Day(Date))

        MsgBox "Date is too far in future. Try again."

    Case Else

        'Good date. Do stuff

        MsgBox "A good date was entered: " & Format(dat, "mm, dd, yy")

    End Select

End If

End Sub

Open in new window

0
 
LVL 81

Accepted Solution

by:
byundt earned 125 total points
ID: 33703800
If you want to test the value in Calendar1, then note that its Value property doesn't update until you pick a day of the month. In other words, I changed the year, and the macro still tested the old value of the date. The same thing applies if you change the month--you need to pick a day on the calendar to get it to update the value.
Private Sub CommandButton3_Click()

Dim dat As Date

dat = Calendar1.Value

Select Case dat

Case Is < DateSerial(Year(Date) - 2, Month(Date), Day(Date))

    MsgBox "Date is too soon. Try again."

Case Is > DateSerial(Year(Date) + 10, Month(Date), Day(Date))

    MsgBox "Date is too far in future. Try again."

Case Else

    'Good date. Do stuff

    MsgBox "A good date was entered: " & Format(dat, "mm, dd, yy")

End Select

End Sub

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

867 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

18 Experts available now in Live!

Get 1:1 Help Now