Format VBA Textbox for Date Only

Dave Punky

Board Regular
Joined
Jan 7, 2010
Messages
133
Hi all,

I'm trying to format a textbox so it will only display a date in the format "dd/mm/yyyy" and if the user has not entered the date in this format then once they exit the textbox it will revert to a blank box.

Annoyingly I can't just use Format(textbox.value,"dd/mm/yyyy") = False because it's just looking at the textbox value.

How can I get around this so it knows that it's either a date in the dd/mm/yyyy format or a blank string?

Many thanks.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
what about something like this

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

'Dim dDate As Date
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
TextBox1.Value = Format(TextBox1.Value, "dd/mm/yyyy")
dDate = TextBox1.Value
End Sub

Example other

Sub var1()
Dim varinput As Variant
vrinput = InputBox("Please enter a number or date or string")
If IsNumeric(varinput) Then
MsgBox (Format(varinput, "#,##0.00;(#,##0.00)"))
ElseIf IsDate(varinput) Then
MsgBox ("the date is " & Format(varinput, "dd mmm yyyy"))
Else
MsgBox ("the string is " & varinput)
End If
End Sub
 
Last edited:
Upvote 0
Thanks for the reply!

It didn't really work too well for me to be honest but I've found this method works best for my use:

Code:
If IsDate(Textbox.Value) = False Then
    Textbox.Value = ""
ElseIf IsNumeric(Format(Textbox.Value, "yyyymmdd")) = False Then
    Textbox.Value = ""
End If

I've got a case statement afterwards evaluating the results too but I've not had any problems doing it this way.
 
Upvote 0

Forum statistics

Threads
1,222,115
Messages
6,164,013
Members
451,867
Latest member
csktwyr

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