I found an issue for date format in excel, which the display format is not as same as the numberformat
in vba
and I have create a test for this case.
Regional Setting for Short Date: MM/dd/yyyy
modules:
Function RetrieveNumberFormat() As String
Dim rng As Range
Set rng = Application.Caller
RetrieveNumberFormat = rng.NumberFormat
Set rng = Nothing
End Function
Function SetDate() As String
Dim rng As Range
Set rng = Application.Caller
SetDate = Format(Now, rng.NumberFormat)
Set rng = Nothing
End Function
In Excel
Col A (Date w default format) | Col B (MM/dd/yyyy) | Col C (dd-mmm-yyyy) | Col D (dd/mm/yyyy) | Col E ([$-404]mm/dd/yyyy)
=Now() | =Now() | =Now() | =Now() | =Now()
=RetrieveNumberFormat() | =RetrieveNumberFormat() | =RetrieveNumberFormat() | =RetrieveNumberFormat() | =RetrieveNumberFormat()
=SetDate() | =SetDate() | =SetDate() | =SetDate() | =SetDate()
Date with default format (Col A):
Result:
May I know why Excel changed System Date Format MM/dd/yyyy
to m/d/yyyy
, and is there a way to workaround?