Date format in Excel VBA coding

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
i have been finding it difficult to manage the date format in program developed by me and being used by many users in my department.
in my program i have asked to enter the date as 14/06/2021.
sometimes the entered date is seen as 14/06/2021 or 06/14/2021 or 14-06-2021 or 06-14-2021
Like for formating numbers in decimals i had used code eg. Format(Userform1.TextBox3, "0.000") to save the number in 3 digits.

Likewise is the following code correct to save a data in the desired format?

Code1:

I am transferring the data to a sheet in cell H15 through userform 2


Sheet1.Range("H15").Value = Format(UserForm2.TextBox3, "dd/mm/yyyy")


Code2:
or should I use the code in Userform _Initialise to assign a format for the cell H15 in the sheet1

eg.

Private Sub UserForm_Initialize()
With Sheet1
Range("H15").NumberFormat = "mm/dd/yyyy"

End With

End Sub

will this work or is there any other better solution.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Typically, you'd allow the users to enter the date in whatever format they normally use, then convert it to a real date value using their regional settings before formatting the cell like this:

Code:
With Sheet1.Range("H15")
    .Value = CDate(UserForm2.TextBox3)
    .Numberformat = "dd/mm/yyyy"
End With
 
Upvote 0
thanks Sir,
will try using this. One small querry....
Is placing this date format code in userform initialisation is correct method or it is to be placed in the main code either in userform or module
 
Upvote 0
There is no point in putting that in the Userform_Initialize event since the date textbox will be empty at that point. ;)
 
Upvote 0
There is no point in putting that in the Userform_Initialize event since the date textbox will be empty at that point. ;)

can I use the same in following types of codes:

.Offset(0, 5).Value = UserForm32.TextBox1
.Offset(0, 5).Value.NumberFormat = "dd/mm/yyyy"
 
Upvote 0
Yes, just remember to use CDate round the textbox value as before.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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