将日期/时间分为两列

编程入门 行业动态 更新时间:2024-10-25 12:21:59
本文介绍了将日期/时间分为两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前有将日期和时间输出到A列的代码,但是我希望它们是分开的(A列中的日期,B列中的时间).我试图通过记录宏并在Excel中进行定界处理来弄清楚这一点,但是它并没有达到我想要的效果.

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 with recording a macro and doing the delimiting process within Excel, but it didn't work like I wanted.

我主要想知道的是这是否可行,还是我应该尝试使用其他编码将它们分开来?

Mainly what I want to know is if this is possible, or I should try alternative coding to separate them to begin with?

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])格式化值.请参阅下面的更新代码(可以根据需要随意更新格式)

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

更多推荐

将日期/时间分为两列

本文发布于:2023-10-27 03:35:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!