Check if date is in future and empty textbox

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hey all,

I use this small code to check if the date entered is in the future or not.
If it is the user gets a messagebox and the cell should empty.

Code:
Private Sub txtStDate_Change()If CDate(txtStDate.Value) > Date Then
    MsgBox "The date of observation is in the future." & vbCrLf & "This is not possible, please correct!"
    txtStDate.Value = ""
    txtStDate.SetFocus
End If
End Sub

Without txtStDate.Value = "" it works. When i added i get error: Type mismatch.
I think because the code runs again when the cells gets emptied.

Someone knows how i can fix this?
 
Hey Guys,

The date that is showed in the textbox is mm/dd/yyyy. While we use dd/mm/yyyy. Guess here is the problem right?
@dmt32
That doesn't resolve the problem. Guess the problem is what i typed above.

DateValue recognizes the order for month, day, and year according to the Short Date format that you specified for your system

So if you enter 10/13/2019 in your textbox, DateValue should, I would have thought, changed it to 13/10/2019.

e.g.
Code:
MsgBox DateValue("10/13/2019") & Chr(10) & Date

maybe another can offer insight to your issue

Dave
 
Last edited:
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Please explain exactly how the value gets into textbox txtStDate
 
Upvote 0
I thought maybe changing the format of txtStDate maybe works but so far no luck;

Code:
txtStDate.Value = Format(txtStDate.Value, "dd/mm/yyyy")
    T = CDate(txtStDate.Value)
 
Upvote 0
That did the trick!

Instead of
Code:
[COLOR=#574123]T = CDate(TxtStDate.Value)[/COLOR]

I changed it into

Code:
T = Format(CDate(Format(txtStDate.Value, "mm/dd/yyyy")))

It seems to work now.

Only the textbox is still showing the "wrong" format (mm/dd/yyyy while it should be dd/mm/yyyy)
 
Upvote 0
I added the following line of code;

Me.txtStDate.Text = Format(Me.txtStDate.Text, "dd/mm/yyyy")

The strange thing is that it works only with the date of today; it shows 13-10-2019 but when i press 12-10-2019 it displays as 10-12-2019

Why?
 
Upvote 0
another option which should work provided that your textbox format is consistently mm/dd/yyyy


call the function like this
Code:
    txtStDate.Value = ReverseDate(txtStDate.Text)


Code:
Private Function ReverseDate(dateText As String)
    Dim d As String, m As String, y As String
    d = Format(Mid(dateText, 4, 2), "00")
    m = Format(Left(dateText, 2), "00")
    y = Format(Right(dateText, 4), "0000")
    ReverseDate = d & "/" & m & "/" & y
End Function


Edit
- this should also give the correct result for y :rolleyes:
Code:
    y = Right(dateText, 4)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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