Userform Textbox - Problem With Future Dates

JustinN1

New Member
Joined
Jan 27, 2011
Messages
46
I have a textbox on a userform. When the userform loads the textbox has the current date in it. I also have spin buttons next to the textbox so users can change the date. When the user selects the submit button on my userform I have it set up to show a message box if the date in the textbox is greater than the current date. This has been working for the past year, except for today. Today is 01/01/19 (mm/dd/yy). I changed the date to yesterday (12/31/18), but my message box comes up and say that future dates are not permitted. I cannot figure out why the message box is showing because 12/31/18 is prior to (less than) 01/01/19.

Here is the code I am using so that future dates cannot be submitted.
Code:
If Me.TextBox5.Value > Format(Date, "MM/DD/YY") Then
MsgBox "A PROBLEM CANNOT BE FUTURE DATED"
Exit Sub
End If

Can someone please help?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I have a textbox on a userform. When the userform loads the textbox has the current date in it. I also have spin buttons next to the textbox so users can change the date. When the user selects the submit button on my userform I have it set up to show a message box if the date in the textbox is greater than the current date. This has been working for the past year, except for today. Today is 01/01/19 (mm/dd/yy). I changed the date to yesterday (12/31/18), but my message box comes up and say that future dates are not permitted. I cannot figure out why the message box is showing because 12/31/18 is prior to (less than) 01/01/19.

Here is the code I am using so that future dates cannot be submitted.
Code:
If Me.TextBox5.Value > Format(Date, "MM/DD/YY") Then
MsgBox "A PROBLEM CANNOT BE FUTURE DATED"
Exit Sub
End If

Can someone please help?
The value in your TextBox and the result from Format(Date, "MM/DD/YY") are both text strings, so you are doing a text comparison, not a "numeric" date comparison.

TextBox: 12/31/18
Format: 01/01/19

The first character of the TextBox is greater than the first character from the Format function when the two values are compared as text, hence, your code fails to work as you wanted. You will need to convert the TextBox value to a real date and then compare that to the unformatted Date function. This should work for you...
Code:
If DateValue(Me.TextBox5.Value) > Date Then
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top