Convert 'yyyymmdd hhmmss' to 'mm/dd/yy hh:mm'

前端 未结 3 588
眼角桃花
眼角桃花 2021-01-22 02:33

I have a row of data (Cell A3 and down) that contains a Unix timestamp in yyyymmdd hhmmss format that I\'m trying to convert to mm/dd/yy hh:mm format <

相关标签:
3条回答
  • 2021-01-22 02:54

    Try this:

    Sub Kyle()
      Dim cell As Range
    
      ThisWorkbook.Activate
    
      For Each cell In Range("A2", Cells(Rows.Count, "A").End(xlUp))
        If cell.Value Like "######## ######" Then
          cell.Value = CDate(Format(cell.Value, "@@@@-@@-@@@@@:@@:@@"))
        End If
      Next cell
    End Sub
    

    Then format the column however you prefer.

    For me, that converts

    20150904 213613
    20150124 194003
    20150404 163056
    20151220 100509
    20150510 213512

    to this:

    09/04/2015 21:36
    01/24/2015 19:40
    04/04/2015 16:30
    12/20/2015 10:05
    05/10/2015 21:35

    0 讨论(0)
  • 2021-01-22 02:57

    This will do it with no looping at all:

    Sub kyle()
        With [a3].Resize([a1048576].End(xlUp).Row - 2)
            .Value = Evaluate("transpose(transpose(DATE(MID(" & .Address & ",1,4),MID(" & .Address & ",5,2),MID(" & .Address & ",7,2)) + TIME(MID(" & .Address & ",10,2),MID(" & .Address & ",12,2),MID(" & .Address & ",14,2))))")
        End With
    End Sub
    

    Note: you can then use whatever number formatting for the dates you please.

    0 讨论(0)
  • 2021-01-22 02:59

    @ExcelHero answered my question via email. The following is the working code for anyone who needs future reference.

        With [a3].Resize([a65536].End(xlUp).Row - 2)
        If Len(.Item(1)) = 15 Then
            .Value = Evaluate("transpose(transpose(DATE(MID(" & .Address & ",1,4),MID(" & .Address & ",5,2),MID(" & .Address & ",7,2)) + TIME(MID(" & .Address & ",10,2),MID(" & .Address & ",12,2),MID(" & .Address & ",14,2))))")
        End If
        .NumberFormat = "mm/dd/yyyy hh:mm"
    End With
    
    0 讨论(0)
提交回复
热议问题