我有一个用户表单,要求用户通过两个单独的组合框cboStartDate,cboStartTime输入特定的日期和时间。用户还必须在文本字段txtDuration中输入持续时间。Excel VBA将时间特定值存储到用户表单的单元格中
保存后,开始日期和时间将被存储在格式化的单元格中[DD/MM/YYYY HH:MM AM/PM]。结束日期和时间将根据持续时间字段计算并存储在具有相同格式的另一个单元格中。例如:
+-----------------------+-----------------------+ | startTime | endTime | +-----------------------+-----------------------+ | 2/4/2012 11:30:00 AM | 2/4/2012 2:00:00 PM | +-----------------------+-----------------------+
但是,在运行完用户表单后,不存储开始时间,并且不计算结束时间。事情是这样的:
+-----------------------+-----------------------+ | startTime | endTime | +-----------------------+-----------------------+ | 2/4/2012 12:00:00 AM | 2/4/2012 12:00:00 AM | +-----------------------+-----------------------+
下面是我的我的VBA代码部分:
Dim iRow As Long
Dim ws As Worksheet
Dim startDate As Date
Dim unFmtStartDuration() As String
Dim startDuration As Double
Dim minTest As Integer
Dim endDate As Date
Dim endDuration As Double
Set ws = Worksheets("EVENTS")
'Search for the last row in the worksheet
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'Date manipulation and set start and end timings
unFmtStartDuration() = Split(cboStartTime.Text, ":")
startDuration = unFmtStartDuration(0)
If unFmtStartDuration(1) = "00" Then
minTest = 0
Else
minTest = unFmtStartDuration(1)
If minTest = 30 Then
startDuration = startDuration + 0.5
End If
End If
startDate = DateValue(DateAdd("h", startDuration, cboDate.Text & " 12:00AM"))
ws.Cells(iRow, 4).Value = startDate
endDuration = txtDuration.Value
endDate = DateValue(DateAdd("h", endDuration, startDate))
ws.Cells(iRow, 5).Value = endDate
所以,我怎样才能得到这部分整理出来?希望在这里得到任何帮助。谢谢。
P.S.想在这里张贴截图,但是我的名声太低了。抱歉。
感谢您的快速答复!我最初并没有考虑'TimeValue()'函数,因为我对使用VBA很陌生。用它在代码和它的美丽。尽管如此,感谢您的帮助!它现在运行得很好......顺便说一下,'DateAdd'函数由于某些原因不能在h中添加0.5 ...因此采用将startTime和endTime分解为几小时和几分钟来代替快速修复。附:希望我可以upvote,再次代表太低... :( – 2012-03-29 16:42:55
+1不错的一个加菲:) – 2012-03-29 17:53:24
谢谢,@SiddharthRout。别担心罗伯特;坚持下去,你会在代表处站起来。 :-) – Gaffi 2012-03-29 18:29:13