使用VBA将dd.mm.yyyy转换为英国日期格式的问题

时间:2019-03-19 09:14:58

标签: excel vba

我的日期格式如下:-

01.01.2019
01.02.2019
01.03.2019
14.03.2019

并且我希望使用VBA转换以dd / mm / yyyy的英国日期格式显示这些数据。

我尝试使用REPLACE,更改了“。”到“ /”,但是将日期输出为:-

01/01/2019
02/01/2019
03/01/2019
14/03/2019

有办法克服吗?

2 个答案:

答案 0 :(得分:4)

下面的代码循环A列并转换B列中的日期。尝试:

Option Explicit

Sub test()

    Dim Year As String, Month As String, Day As String, WholeString As String
    Dim i As Long, Lastrow As Long

    With ThisWorkbook.Worksheets("Sheet1")

        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 1 To Lastrow

            WholeString = .Range("A" & i).Value
            Year = Right(WholeString, 4)
            Month = Mid(WholeString, 4, 2)
            Day = Left(WholeString, 2)

            With .Range("B" & i)
                .Value = CDate(Day & "/" & Month & "/" & Year)
                .NumberFormat = "dd/mm/yyyy"
            End With

        Next i

    End With

End Sub

答案 1 :(得分:1)

这对您有什么作用?我很好奇它是否会为您返回dd/mm/yyyy

With ActiveSheet.UsedRange.Columns("A").Cells
    .TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, FieldInfo:=Array(0, xlDMYFormat)
    .NumberFormat = "dd/mm/yyyy"
End With