Splitting Date/Time into 2 columns

前端 未结 1 1874
逝去的感伤
逝去的感伤 2021-01-22 21:02

I currently have code that outputs the date and time into column A, but I would like for them to be separate (Date in column A, Time in Column B). I tried to figure this out wit

1条回答
  •  长情又很酷
    2021-01-22 21:28

    You can use Format(Expression,[format]) to format the values before putting it in the cell. See the updated code below (feel free to update the formats as you see fit)

    Sub Macro1()
    
    Dim dTime As Date
    Dim x As Integer
    
    x = 1
    
    For dTime = "3/01/2013 12:00:00 AM" To "3/02/2013 11:55:00 PM" Step TimeValue("00:05:00")
        ' Sets the date in the cell of the first column
        Cells(x,1).Value = DateValue(dTime)
    
        ' Sets the time in the cell of the second column
        Cells(x,2).Value = TimeValue(dTime)
    
        ' Increment the row position
        x = x + 1
    Next dTime
    End Sub
    

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