rhombus4
Well-known Member
- Joined
- May 26, 2010
- Messages
- 586
- Office Version
- 365
- 2016
- Platform
- Windows
I want to enter a value into a textbox and check to make sure it is a valid date. Then it transfer to a cell when I hit a command button
If I start to enter the date then decide I dont need it I want to be able to delete what I have entered and still be able to transfer that textbox to a cell
or even If I dont enter anything in it at all let it transfer to the cell as blank
The code below works fine to check if a date is valid or not, although If I start to enter a date and then decide I don't want to on this particular occasion and hit the backspace key/ It wont let me move on to the rest of the form. I still get the message Date is not Valid.
I did try changing the cancel = False which seemed ok but then got an error when I wanted to transfer the date to a cell
run time error 13 type mismatch think because its trying to enter a blank "" in a cell
tried both below but got error when it tried to transfer to a cell when i had cleared the textbox after I had changed the code to cancel = false
If I start to enter the date then decide I dont need it I want to be able to delete what I have entered and still be able to transfer that textbox to a cell
or even If I dont enter anything in it at all let it transfer to the cell as blank
The code below works fine to check if a date is valid or not, although If I start to enter a date and then decide I don't want to on this particular occasion and hit the backspace key/ It wont let me move on to the rest of the form. I still get the message Date is not Valid.
I did try changing the cancel = False which seemed ok but then got an error when I wanted to transfer the date to a cell
run time error 13 type mismatch think because its trying to enter a blank "" in a cell
tried both below but got error when it tried to transfer to a cell when i had cleared the textbox after I had changed the code to cancel = false
Range("A2") = CDate(Me.textbox1.Value)
Range("A2") = Format(Me.textbox1, "dd/mmm/yy").Value
VBA Code:
Private Sub textbox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(Me.textbox1.Text) Then
MsgBox "Date is not Valid"
Cancel = True
Exit Sub
End If
Me.textbox1 = Format(CDate(Me.textbox1), "dd mm yy")
End Sub
Last edited by a moderator: