2

我目前有将日期和时间输出到 A 列的代码,但我希望它们是分开的(A 列中的日期,B 列中的时间)。我试图通过录制宏并在 Excel 中执行定界过程来解决这个问题,但它并没有像我想要的那样工作。

主要我想知道的是这是否可能,或者我应该尝试替代编码来将它们分开?

Sub Macro1()

Dim dTime As Date
Range("A:A").Select
Selection.NumberFormat = "mm/dd/yyyy hh:mm:ss AM/PM"

Range("A1").Select
Set Cell = [A1]

For dTime = "3/01/2013 12:00:00 AM" To "3/02/2013 11:55:00 PM" Step "00:05"
ActiveCell.Value = dTime
ActiveCell.Offset(1, 0).Select

Next dTime

End Sub
4

1 回答 1

4

您可以Format(Expression,[format])在将值放入单元格之前对其进行格式化。请参阅下面的更新代码(随意更新您认为合适的格式)

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
于 2013-07-18T19:31:16.507 回答