VBA Dateadd格式-需要总分钟数

VBA Dateadd格式-需要总分钟数,vba,formatting,Vba,Formatting,我在Microsoft Excel中有一个用户表单,我想用作秒表。但是,“hh:mm”的格式不允许在返回00:00时超过23:59 Private Sub SpinButton2_SpinUp() If InsertEvent.TextBox1 = vbNullString Then InsertEvent.TextBox1 = "00:00" Else InsertEvent.TextBox1.Value = Format(DateAdd("n", 1, InsertEven

我在Microsoft Excel中有一个用户表单,我想用作秒表。但是,“hh:mm”的格式不允许在返回00:00时超过23:59

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),但它是否是无限的并不重要。

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

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

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开始,然后单击“旋转”按钮,它将从00:00直接开始…这样做的能力将丢失,更新如上。