Excel/VBA converts dates to American format on transpose/paste from VBA

前端 未结 1 1003
臣服心动
臣服心动 2020-12-20 06:53

I\'m working in a British context right now. Which I think I have set up in regional settings (Windows 7 in a corporate context, Excel 2016, ver 1803 (I think this may also

相关标签:
1条回答
  • 2020-12-20 07:39

    If you do not specify a cell format, it appears to be replaced with a date format for that country format. It is likely that you want to format the cell appropriately.

    According to my tests, it seems to automatically recognize when I set the date value, specify the cell format as UK or US, and then put the value back into the variable.

    Our region has a default date format of yyyy-mm-dd.

    test code

    Sub test()
        Dim p_dtTermArray() As Date
        Dim vR() As Long
        Dim lgnumterms As Integer
        lgnumterms = 2
    
        ReDim p_dtTermArray(1 To lgnumterms)
        p_dtTermArray(1) = DateSerial(2018, 10, 1)
        p_dtTermArray(2) = DateSerial(2018, 11, 1)
        ReDim vR(1 To lgnumterms)
    
        vR(1) = DateSerial(2018, 10, 1)
        vR(2) = DateSerial(2018, 11, 1)
        With Range("a1").Resize(1, 2)
            .Value = p_dtTermArray
            .NumberFormatLocal = "dd-mm-yyyy"
        End With
    
        With Range("a2").Resize(1, 2)
            .Value = vR
            .NumberFormatLocal = "mm-dd-yyyy"
        End With
    
        Dim vDB, vDB2
            vDB = Range("a1").Resize(1, 2)
            vDB2 = Range("a2").Resize(1, 2)
    
    End Sub
    

    Sheet image

    Locals window

    I found that when I transpose an array of date format, it changes to a character format. A way to prevent this would be to convert the matrix into code.

    Sub test()
        Dim p_dtTermArray() As Date
        Dim vR() As Long
        Dim lgnumterms As Integer
        Dim r As Long, c As Long, i As Long, c As Long
        lgnumterms = 2
    
        ReDim p_dtTermArray(1 To lgnumterms)
        p_dtTermArray(1) = DateSerial(2018, 10, 1)
        p_dtTermArray(2) = DateSerial(2018, 11, 1)
        ReDim vR(1 To lgnumterms)
    
        vR(1) = DateSerial(2018, 10, 1)
        vR(2) = DateSerial(2018, 11, 1)
        With Range("a1").Resize(1, 2)
            .Value = p_dtTermArray
            .NumberFormatLocal = "dd-mm-yyyy"
        End With
    
        With Range("a2").Resize(1, 2)
            .Value = vR
            .NumberFormatLocal = "mm-dd-yyyy"
        End With
    
        Dim vDB, vDB2, vD() As Long, vD2() As Long
    
            vDB = Range("a1").Resize(1, 2).Value
            vDB2 = Range("a2").Resize(1, 2).Value
    
    
        r = UBound(vDB, 1)
        c = UBound(vDB, 2)
    
        ReDim vD(1 To c, 1 To r)
        ReDim vD2(1 To c, 1 To r)
        For i = 1 To r
            For j = 1 To c
                vD(j, i) = vDB(i, j)
                vD2(j, i) = vDB2(i, j)
            Next j
        Next i
        Range("d1").Resize(2) = vD
        Range("f1").Resize(2) = vD2
    
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题