我目前有将日期和时间输出到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更多推荐
将日期/时间分为两列
发布评论