Excel VBA,通过时间选择器显示时间值

00jrzges  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(240)

我创建了一个用户表单,用户必须在其中输入一些时间值,这些时间值例如是产品必须处理多长时间。我有一个代码,几乎是我希望它做什么,只有当它达到24小时,它重置为0。但输入需要24小时以上(例如:36:59:59(其中mm & ss的最大值= 59,hh最大值应为99)。
有人能帮助我如何改变这一点吗?

Private mtmPosition1 As tmPosition1

Private Const msFMTTIME1 As String = "[$-409]hh:mm:ss"
Private Const miRIGHTARROW1 As Integer = 39
Private Const miLEFTARROW1 As Integer = 37

Private Const mdHOUR1 As Double = 1 / 24
Private Const mdMINUTE1 As Double = 1 / 24 / 60
Private Const mdSECOND1 As Double = 1 / 24 / 60 / 60

Private Enum tmPosition1
    tmPositionHour1
    tmPositionMinute1
    tmPositionSecond1

End Enum

Private Sub sbTime1_SpinDown()
    
    If Me.IsHour1 Then
        Me.tbxTimePicker1.Text = Format(1 + TimeValue(Me.tbxTimePicker1.Text) - mdHOUR1, msFMTTIME1)
        SelectHour1
    ElseIf Me.IsMinute1 Then
        Me.tbxTimePicker1.Text = Format(1 + TimeValue(Me.tbxTimePicker1.Text) - mdMINUTE1, msFMTTIME1)
        SelectMinute1
    ElseIf Me.IsSecond1 Then
        Me.tbxTimePicker1.Text = Format(1 + TimeValue(Me.tbxTimePicker1.Text) - mdSECOND1, msFMTTIME1)
        SelectSecond1

    End If
    
End Sub

Private Sub sbTime1_SpinUp()

    If Me.IsHour1 Then
        Me.tbxTimePicker1.Text = Format(1 + TimeValue(Me.tbxTimePicker1.Text) + mdHOUR1, msFMTTIME1)
        SelectHour1
    ElseIf Me.IsMinute1 Then
        Me.tbxTimePicker1.Text = Format(1 + TimeValue(Me.tbxTimePicker1.Text) + mdMINUTE1, msFMTTIME1)
        SelectMinute1
    ElseIf Me.IsSecond1 Then
        Me.tbxTimePicker1.Text = Format(1 + TimeValue(Me.tbxTimePicker1.Text) + mdSECOND1, msFMTTIME1)
        SelectSecond1

    End If
    
End Sub

Private Sub tbxTimePicker1_Enter()
    
    With Me.tbxTimePicker1
        .SelStart = 0
        .SelLength = 2
    End With
    
    mtmPosition1 = tmPositionHour1
    
End Sub

Private Sub tbxTimePicker1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
    If KeyCode = miRIGHTARROW1 Then
        If Me.IsHour1 Then
            SelectMinute1
        ElseIf Me.IsMinute1 Then
            SelectSecond1
        End If
    ElseIf KeyCode = miLEFTARROW1 Then
        If Me.IsSecond1 Then
            SelectMinute1
        Else
            SelectHour1
        End If
    Else
        If Me.IsHour1 Then
            SelectHour1
        ElseIf Me.IsMinute1 Then
            SelectMinute1
        ElseIf Me.IsSecond1 Then
            SelectSecond1
        End If
    End If
    
End Sub

Private Sub tbxTimePicker1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    
    If Me.tbxTimePicker1.SelStart < 3 Then
        SelectHour1
    ElseIf Me.tbxTimePicker1.SelStart < 6 Then
        SelectMinute1
    ElseIf Me.tbxTimePicker1.SelStart < 9 Then
        SelectSecond1
    End If
    
End Sub


Public Property Get IsHour1() As Boolean
    
    IsHour1 = mtmPosition1 = tmPositionHour1
    
End Property

Public Property Get IsMinute1() As Boolean
    
    IsMinute1 = mtmPosition1 = tmPositionMinute1
    
End Property

Public Property Get IsSecond1() As Boolean
    
    IsSecond1 = mtmPosition1 = tmPositionSecond1
    
End Property

Private Sub SelectMinute1()
    
    With Me.tbxTimePicker1
        .SetFocus
        .SelStart = 3
        .SelLength = 2
    End With
    
    mtmPosition1 = tmPositionMinute1
    
End Sub

Private Sub SelectHour1()
    
    With Me.tbxTimePicker1
        .SetFocus
        .SelStart = 0
        .SelLength = 2
    End With
    
    mtmPosition1 = tmPositionHour1
    
End Sub

Private Sub SelectSecond1()
    
    With Me.tbxTimePicker1
        .SetFocus
        .SelStart = 6
        .SelLength = 2
    End With

    mtmPosition1 = tmPositionSecond1
    
End Sub
jmo0nnb3

jmo0nnb31#

在不尝试修改您的代码的情况下,我建议执行以下操作:
让用户输入,SpinDownSpinUp控制“过程持续时间”为三个数字(整数),0-99(小时)、0-59(分钟)和0-59(秒)。不要将这些值视为时间值,仅视为简单的数字。
如果适用,让用户输入“过程开始时间”,使用单独的SpinDown/SpinUp控件作为真实时间值。
如适用,显示“过程结束时间”时,使用简单文本框显示开始时间+持续时间作为计算的结束时间(天、小时、分钟、秒)

相关问题