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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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