vba - Excel VBA将时间特定值从用户窗体存储到单元格中

标签 vba datetime excel date-formatting

我有一个用户表单,要求用户通过两个单独的组合框(cboStartDate、cboStartTime)输入特定的日期和时间。用户还必须在文本字段 txtDuration 中输入持续时间。

保存后,开始日期和时间将存储在格式化单元格中 [DD/MM/YYYY HH:MM AM/PM]。结束日期和时间将从持续时间字段计算并存储在具有相同格式的另一个单元格中。像这样的事情:

+-----------------------+-----------------------+
| startTime             | endTime               |
+-----------------------+-----------------------+
| 2/4/2012  11:30:00 AM | 2/4/2012  2:00:00 PM  |
+-----------------------+-----------------------+

However, after running the userform through, the start time is not stored, and the end time is not calculated. Something like this:

+-----------------------+-----------------------+
| startTime             | endTime               |
+-----------------------+-----------------------+
| 2/4/2012  12:00:00 AM | 2/4/2012  12:00:00 AM |
+-----------------------+-----------------------+

Below is my part of my VBA code:

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

那么我该如何解决这部分问题呢?非常感谢这里的任何帮助。谢谢。

附注想在这里发布屏幕截图,但我在这里的声誉太低了。抱歉。

最佳答案

看起来您只添加了 minTest = 30 时的时间,但该值可能变化很大。此外,在一个实例中,您在引用 unFmtStartDuration 时比较一个字符串和另一个数字,这可能有效,但在阅读代码时会令人困惑。

要遵循当前的方法,请使用

startDuration = Val(unFmtStartDuration(0) + Round(Val(unFmtStartDuration(1)) / 60, 2)

替换这个

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

这将获取任何时间并将其转换为您正在使用的十进制形式,而不是依赖于 30 匹配。 (除非你特别需要。如果是这样,请这么说,因为我认为这仍然可以通过舍入技巧来安排。)

但是,我认为更好的选择是使用

startDuration = TimeValue(cboStartTime.Text) * 24

因此不涉及其他数学或检查。

此外,除非 cboStartTime.Text(以及随后的 startDuration)大于 24 小时,否则

startDate = DateValue(DateAdd("h", startDuration, cboDate.Text & " 12:00AM"))

将始终返回 cboDate.Text 中指定的日期,并隐含 12:00:00 AM。要纠正这个问题,您需要更改为

startDate = DateAdd("h", startDuration, cboDate.Text & " 12:00AM")

我认为还有一些问题需要解决,但希望这能让您朝着正确的方向前进......

关于vba - Excel VBA将时间特定值从用户窗体存储到单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927188/

相关文章:

php - 无法从 PHP 中的 DateTime 获取上个月 - 这是一个(相当大的)错误吗?

在 C 中将日期和时间转换为毫秒

excel - 在 Access 中使用 Excel 电子表格

excel - 为什么除非我双击单元格,否则这个公式不会计算?

excel - 在 VBA 中创建隐藏类成员和成员函数

java - 在 C# 和 Java 之间传递 DateTime?

VBA 字。超链接的 HighlightColorIndex 返回 9999999

excel - 使用列标题和子标题填充 TreeView

sql - MS Access SQL,没有数字字段的 Rnd 函数

vba - 使用 VBA 宏选择和复制 Outlook 电子邮件正文