Change date in Userform to "dd/mm/yy"

manny88

New Member
Joined
Oct 28, 2016
Messages
33
Hi Guys,

I am having issues with the date appearing the way I need it to in my userform textbox. At the moment any entry that I make as "dd/mm/yyyy" is converted to the spreadsheet as "mm/dd/yyyy"

i.e 09/06/2020 changes to 06/09/2020

The code I am using is this directly below

VBA Code:
Cells(EmptyRow, 4).value = DateTextBox.Value

I have tried a few variations of adding DateTextBox.Value = Format(DateTextBox.Value, "dd/mm/yyyy") without success.

Can you help?

P.S. a bonus help would be something to prevent date entry being entered in any other way to ensure consistency in the data
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try

VBA Code:
Cells(EmptyRow, 4).Value = DateValue(DateTextBox.Value)

Note - DateValue Function will error if no valid date exists in your textbox
Function recognises the order of day, month year according to the short date format specified in your system which should be your bonus - maybe!

Dave
 
Upvote 0
Try

VBA Code:
Cells(EmptyRow, 4).Value = DateValue(DateTextBox.Value)

Note - DateValue Function will error if no valid date exists in your textbox
Function recognises the order of day, month year according to the short date format specified in your system which should be your bonus - maybe!

Dave

How was it so simple?! Thank you so much, Dave!
 
Upvote 0
Regards to the bonus question what I was thinking is like a check with a possible is function , perhaps.

So if the date is entered in the American format it would reject with a error/message box until it is entered as "dd/mm/yyyy"
 
Upvote 0
How was it so simple?! Thank you so much, Dave!

Excel VBA full of hidden gems - just ensure you manage the possibility of an invalid date (including empty textbox)

Many thanks for feedback

Dave
 
Upvote 0
Regards to the bonus question what I was thinking is like a check with a possible is function , perhaps.

So if the date is entered in the American format it would reject with a error/message box until it is entered as "dd/mm/yyyy"

you could try something like following & see if helps

VBA Code:
Private Sub DateTextBox_AfterUpdate()
    Dim MyDate As Date
    With DateTextBox
        MyDate = DateSerial(Year(.Value), Month(.Value), Day(.Value))
    End With
    DateTextBox.Value = Format(MyDate, "dd/mm/yyyy")
End Sub

Dave
 
Upvote 0
neglected to add that suggestion most likely will some shortcomings but if you want to fully ensure date values get entered in correct order then as an idea, you could add 3 textboxes on your userform for day month & year - then just combine the values in to date format.

Dave
 
Upvote 0
neglected to add that suggestion most likely will some shortcomings but if you want to fully ensure date values get entered in correct order then as an idea, you could add 3 textboxes on your userform for day month & year - then just combine the values in to date format.

Dave
Thanks Dave. I'm going to give that tip a go!

However, the issue this has now created is if the user wants to enter "N/A", the code DateValue(DateTextBox.Value) doesn't allow it comes up as a 'type mismatch'

Additionally,

Cells(empty Row, 13.Value = Format(Now, "dd/mm/yyyy HH:mm" intended as a timestamp of the entry is also reversing the day and months
 
Upvote 0
Thanks Dave. I'm going to give that tip a go!

However, the issue this has now created is if the user wants to enter "N/A", the code DateValue(DateTextBox.Value) doesn't allow it comes up as a 'type mismatch'
I did point out in my post that DateValue function will error if no valid date exists in textbox
If you are going to allow invalid values to be entered then you will need to update code to manage this - you can use the IsDate function to resolve

something like following maybe

VBA Code:
With DateTextBox
    If IsDate(.Value) Then
       Cells(EmptyRow, 4).Value = DateValue(.Value)
    Else
       Cells(EmptyRow, 4).Value = .Value
     End If
  End With

Additionally,

Cells(empty Row, 13.Value = Format(Now, "dd/mm/yyyy HH:mm" intended as a timestamp of the entry is also reversing the day and months

You can apply NumberFormat to range & see if this resolves your issue - e.g.

VBA Code:
With Cells(EmptyRow, 13)
    .Value = Now()
    .NumberFormat = "dd/mm/yyyy HH:mm"
 End With

Dave
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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