excel - 将日期/时间拆分为 2 列

标签 excel vba

我目前有将日期和时间输出到 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

最佳答案

您可以在将值放入单元格之前使用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

关于excel - 将日期/时间拆分为 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731795/

相关文章:

excel - 双击复制单元格并自动粘贴到另一张纸上的不同单元格

Excel 区分大小写的 COUNTIF 带通配符 : native function?

excel - 运行大量 VBA 后出现打印预览问题

excel - VBA excel函数

regex - 如何使用 excel 正则表达式从字符串中提取广告尺寸

vba - 设置十六进制和十进制颜色的行为不同

VBA find 在用户表单中返回 "nothing"

vba - 如何通过枚举值来声明(初始化)一维和二维固定大小的数组?

excel - EXCEL VBA 中是否可以将多维数组作为参数传递?

VBA - 在子程序中调用 Dir() 时使用 Dir() 循环