【问题标题】:VBA Dateadd Format - Need In Total MinutesVBA Dateadd 格式 - 需要总分钟数
【发布时间】:2016-12-22 10:56:30
【问题描述】:

我在 Microsoft Excel 中有一个用户表单,我想将其用作秒表。然而,“hh:mm”的格式不允许它超过 23:59,因为它会回到 00:00

Private Sub SpinButton2_SpinUp()

If InsertEvent.TextBox1 = vbNullString Then
InsertEvent.TextBox1 = "00:00"

Else

InsertEvent.TextBox1.Value = Format(DateAdd("n", 1,       InsertEvent.TextBox1.Value), "hh:mm")
'InsertEvent.TextBox1.Value = TimeValue("mm:ss")
'InsertEvent.TextBox1.Value = Format(InsertEvent.TextBox1.Value, "hh:mm")

End If

End Sub 

是否有任何格式化它可以作为总分钟的时钟工作?理想情况下,我需要大约 125 分钟左右(125:00),但它是否无限制也没关系。

【问题讨论】:

    标签: vba formatting


    【解决方案1】:

    您不能为此使用内置的日期/时间函数,因为您需要一个不是日期/时间的表示。

    假设您想将微调器值读入文本框:

    Private Sub SpinButton2_SpinUp()
        Dim minutes As Integer: minutes = Val(InsertEvent.SpinButton2.Value)
        Dim hh As Integer:      hh = minutes \ 60
        Dim mm As Integer:      mm = minutes - (hh * 60)
    
        InsertEvent.TextBox1.Text = Format$(hh, "00") & ":" & Format$(mm, "00")
    End Sub
    

    要使用从文本框中手动输入的值作为启动/停止点,您需要将“hh:mm”重新解析为分钟,例如在文本框退出事件中:

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        If (IsNumeric(TextBox1.Text)) Then
            '// entering a number only assumes its minutes
            SpinButton2.Value = TextBox1.Text
            Exit Sub
        End If
    
        Dim hhmm() As String: hhmm = Split(TextBox1.Text, ":")
        If (UBound(hhmm) = 1) Then
            If (IsNumeric(hhmm(0)) And IsNumeric(hhmm(1))) Then
                SpinButton2.Value = (hhmm(0) * 60) + hhmm(1)
                Exit Sub
            End If
        End If
    
        SpinButton2.Value = 0
    End Sub
    

    (应该为溢出/超过 spinners .Max 属性添加错误检查)

    【讨论】:

    • 谢谢!看起来这几乎是我需要的,但是如果我在文本框中输入一个数字并想使用旋转按钮增加/减少它不起作用。它与我原来的 dateadd 一起工作......例如,如果我在时钟中输入 20:00 开始,然后单击“spin up”按钮,它直接从 00:00 开始......
    • 失去了做这件事的能力,上面更新了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多