2016-05-16 122 views
0

我想利用用户输入(即,“5”)和使该进入的00:05:00的格式(HH:MM:SS) ,其中输入的数字是计时器中要使用的分钟数。然后程序会自动启动一个具有指定时间的计时器。时间将按原样显示在B10中,不需要格式化,但定时器将显示在单元格“I1”中。VBA取用户输入(整数),并将其输入到计时器Excel 2007中

'FUNCTION FOR FORMATTING THE INPUT 
Public Function RetTime(IntTime As Integer) As Date 
RetTime = TimeSerial(Int(IntTime/10000), Int((IntTime Mod 10000)/100), (IntTime Mod 100)) 
End Function 


'ACCEPTING THE TIME INTERVAL 
Sub TimeInterval() 
TimeIntervals = InputBox("How long for the intervals?") 
Range("B10").Value = TimeIntervals 
Range("A2").Select 

'setting that time interval to the " I1 " cell 
I1 = RetTime(TimeIntervals) 
Range("I1").Value = I1 

NumOfInterval 

End Sub 


'THE COUNTDOWN TIMER: 
Sub StartTimer() 
    Future = Now + TimeSerial(0, 0, 1) 
    Application.OnTime earliestTime:=Future, procedure:="nextTime", _ 
     schedule:=True 
End Sub 

'From the formatted input that was placed in "I1", make that into a timer 
Sub nextTime() 
    Sheet1.Range("I1").TimeValue = Sheet1.Range("I1").TimeValue - TimeValue("00:00:01") 
    StartTimer 
End Sub 

Sub StopTimer() 
    On Error Resume Next 
    Application.OnTime earliestTime:=Future, _ 
     procedure:="nextTime", schedule:=False 
End Sub 

回答

0

如果他们遵循输入框中的格式,您可以使用此代码。 在这个例子中00:05:00的用户输入将导致变量timer = 5

Sub ashTime() 

    Dim timer As String 

    timer = minute(InputBox("Enter minutes as HH:MM:SS")) 

    MsgBox timer 

End Sub