Behavior:
When I transpose a 1 dimensional array, containing dates, in order to print them to a sheet in it's entirety, some dates are changed from the dd/mm/yy
to mm/dd/yyyy
.
In particular, when the day of the month:
- is less than or equal to 12, such as
January 2, 2016 (02/01/16)
, orMay 11, 2016 (11/05/16)
, then the date is printed with the date formatmm/dd/yy
and is aligned right. - is greater than or equal to 13, such as
April 23, 2016 (23/04/16)
, orDecember 17, 2016 (17/12/16)
, then the date is printed with the date formatdd/mm/yyyy
and is aligned left.
When I use a for loop to print each date separately, or I do not transpose the array and print each date in the first row of each column however, all dates are printed with the format dd/mm/yy
and all dates are aligned right.
Additional information:
I have:
- Windows 8.1 (English U.S.)
- Office 365 Student (English U.S) (Excel 2016 32 bit)
- Locale setting: Netherlands
Code:
Option Explicit
Sub TransposeDatesArray()
Dim arrDates() As Date
Dim i As Variant
ReDim arrDates(0)
For i = CDate("Januari 01, 2016") To CDate("December 31, 2016")
If UBound(arrDates) = 0 Then
ReDim arrDates(1 To 1)
Else
ReDim Preserve arrDates(1 To UBound(arrDates) + 1)
End If
arrDates(UBound(arrDates)) = i
Next
With ThisWorkbook.Worksheets(1)
.Cells.Delete
.Cells(1, 1).Resize(UBound(arrDates)).Value = Application.Transpose(arrDates)
.Cells(1, 2).Resize(UBound(arrDates)).Value2 = Application.Transpose(arrDates)
.Cells(1, 3).Resize(UBound(arrDates)).Formula = Application.Transpose(arrDates)
For i = LBound(arrDates) To UBound(arrDates)
.Cells(i, 4).Value = arrDates(i)
.Cells(i, 5).Value2 = arrDates(i)
.Cells(i, 6).Formula = arrDates(i)
Next
End With
End Sub
.Value2
or.Formula
)? It seems to me that not all dates are recognized as such..Value2
and.Formula
results..Value2
and then change the formatting of the cells to the desired date format. Note, that date are essentially numbers: 42409 = 42409 days after 00.01.1900 and 0.75 = three quarters of a day = 18:00. Change the format using.NumberFormat = "[$-409]d-mmm-yy;@"
(for example).long
, storing the dates as the number and then use.Numberformat = "dd/mm/yy"
on the range to which the numbers were printed./
indicates to your NL Excel a foreign date when it is normally expecting a-
according to this: en.wikipedia.org/wiki/Date_format_by_country. So, I assume that it tries to evaluate the date as US date using mm/dd/yyyy.