Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Date Format - month/day/year

Tags:

excel

vba

I want to produce todays date like this: 01/23/2016 month/day/year. I wrote the below code but the result is 23/01/2016. Can anyone help me? Thanks

ActiveSheet.Cells(1, 2) = Format(Date, "mm/dd/yyyy")

like image 665
Cadman Avatar asked Jan 07 '23 18:01

Cadman


1 Answers

The code you use enters something into an Excel cell. Excel recognises that it is a date and will display the date according to the default settings.

In order to display the date with your preferred format, you need to format the cell that displays the date, not the data entry string.

ActiveSheet.Cells(1, 2) = Date
ActiveSheet.Cells(1, 2).NumberFormat = "mm/dd/yyyy"
like image 164
teylyn Avatar answered Jan 15 '23 11:01

teylyn