1

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), or May 11, 2016 (11/05/16), then the date is printed with the date format mm/dd/yy and is aligned right.
  • is greater than or equal to 13, such as April 23, 2016 (23/04/16), or December 17, 2016 (17/12/16), then the date is printed with the date format dd/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

Result:

Result

8
  • You are merely showing us the formatted values. Maybe you can add a 2nd screenshot next to it which shows us the un-formatted values (what is really stored in these cell as .Value2 or .Formula)? It seems to me that not all dates are recognized as such.
    – Ralph
    Commented Mar 16, 2016 at 18:16
  • @Ralph, I've updated the code and added a screenshot to show the .Value2 and .Formula results. Commented Mar 16, 2016 at 19:21
  • Perfect. In that case I'd transpose the .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).
    – Ralph
    Commented Mar 16, 2016 at 19:40
  • @Ralph The thing I'm looking for is the answer to why when the array is transposed Excel suddenly behaves oddly with dates but behaves normally when looping through the array or simply not transposing the array, . The easiest workaround is declaring the array as long, storing the dates as the number and then use .Numberformat = "dd/mm/yy" on the range to which the numbers were printed. Commented Mar 16, 2016 at 20:21
  • Whenever you enter anything into a cell Excel automatically tries to evaluate what you want and formats it accordingly. The same happens when you are copying and pasting data. Yet, the date format is somewhat misleading. The / 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.
    – Ralph
    Commented Mar 16, 2016 at 20:28

4 Answers 4

3

After doing some more research, I have found the following:

It would seem that the Application.Transpose(arrDate) transposes not only the array, but also date values when they are stored as actual date.

Consider the date , 42373 (January 4, 2016)

  • Debug.Print Format(CDate(42373), "mmmm d, yyyy")
    • produces januari 4, 2016
  • Debug.Print Application.Transpose(Format(CDate(42373), "mmmm d, yyyy"))
    • produces april 1, 2016

It appears that a date value can be transposed when stored as an actual date. The transposing effectively reorders the date from day/month to month/day after which the the month becomes the day and the day becomes the month because the system still uses the day/month format. This can only be done if the day of the month is 12 or less, because after transposing the day becomes the month.

2
  • While I cannot confirm your findings on my computer (both debug print produce the same result being January 4, 2016) I am glad that you have found a working solution.
    – Ralph
    Commented Mar 17, 2016 at 16:57
  • I just posted a variation on your question ( stackoverflow.com/questions/53348333/… ) and someone directed me here. So far I can't fix it and am avoiding Transpose instead. One point I can contribute is that if look in the locals window at a transposed date array--you can see that the Dates become Strings after Transpose. For whatever reason, I guess Transpose makes a new array, but doesn't make it the same type. What's still weird is that those Locals Strings are in the correct format. But get misread on pasting.
    – RomnieEE
    Commented Nov 18, 2018 at 23:03
1

I hit this problem, but interestingly, the date switch (from dd/mm to mm/dd) on transpose only happened when I ran the macro from a toolbar button; if I ran it from inside the VBA editor, or from the Developer menu>Macros dialogue box, it worked fine. To make it work no matter where I ran the macro from, I added a function before the transpose to convert all dates to strings (looping through my array and using the CStr function), and then converted back from strings to dates after the transpose (another loop and CDate function) - the loops having to be slightly different to account for the transposed dimensions.

1

Transposing array with Application.Transpose() transforms the "Date" data type into "String". Not able to explain why. So my suggestion is to transform the Column with "Date" data type into "Long" before you do the Application.Transpose() (simply using CLng). After that you can either transform it back to "Date" or just set the number format as "Date" for the cells, where you would like to paste the array.

0

I resolve this issue using .FormulaLocal to write back the array in the sheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.