简体   繁体   中英

Reformat date from YYYY.MM.DD to DD/MM/YYYY

I import a date into an Excel sheet from a text file. The date has the form: YYYY.MM.DD.

I want to reverse the date so as to be: DD/MM/YYYY.

I have tried NumberFormat, Format and some other subroutines.

Here is the code:

Sub ImportRange()

    Dim Filename As String
    Dim Data

    Dim Pos As Integer

    On Error Resume Next

    Filename = Application.DefaultFilePath & "\putty.log"

    Open Filename For Input As #1

    If Err <> 0 Then
        MsgBox "Not found:" & Filename, vbCritical, "ERROR"
        Exit Sub
    End If

    Application.ScreenUpdating = False

    Line Input #1, Data

    Close #1

    Pos = InStr(Data, "log")
    Sheets("ÐÉÓÔÏÐÏÉÇÔÉÊÏ").Range("B20").Value = Mid(Data, Pos + 4, 10) 

    Application.ScreenUpdating = True

End Sub

I have inserted a sample formula for the conversion let me know if this helps.

If cell A2 has the given date you can insert the given formula in B2 =DATE(VALUE(LEFT(A2,4)),VALUE(MID(A2,6,2)),VALUE(RIGHT(A2,2)))

确保将日期实际输入的单元格格式化为日期

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM