Excel - date format issue in vba

WaqarCoder

New Member
Joined
Jan 29, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I am using below code in my userform in order to put todays date by default. The below code sets the date in the given format in my textbox :-

VBA Code:
Private Sub UserForm_Initialize()
txt_date.Value = Format(Date, "d/m/yyyy")
End Sub

But when I click Add data, then in the sheet1 the Date column is showing m/d/yy? I even set the Date column format as *dd/mm/yy but it still same issue.
How can I solve this?
 

Attachments

  • date form.PNG
    date form.PNG
    2.6 KB · Views: 13
  • date sheet.PNG
    date sheet.PNG
    536 bytes · Views: 12

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,
what comes out of your textbox is a string so try coercing the text date with DateValue conversion function

example

Rich (BB code):
Cells(1, 10).Value = DateValue(Me.txt_date.Value)

Do be aware that if no valid date exists in your textbox the function will error.

Dave
 
Upvote 0
Hi,
what comes out of your textbox is a string so try coercing the text date with DateValue conversion function

example

Rich (BB code):
Cells(1, 10).Value = DateValue(Me.txt_date.Value)

Do be aware that if no valid date exists in your textbox the function will error.

Dave
Where shall i put this code?
 
Upvote 0
I gave an example of how you can use the DateValue function but if you are still unsure publish your forms code here

Dave
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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