PDA

View Full Version : time sum



du-gy
09-27-2009, 04:59 AM
Hello,
this is my first post on this forum (nice green colour:) )
I've some issue with vba code:


Option Explicit
Dim Start As Single
Dim Czas As Single

'------------------------------------------------------------------------------------------------------------------
Private Sub UserForm_Activate()

Label1.Caption = ""
Label2.Caption=""
CommandButton1.Caption = "Start"
CommandButton2.Caption = "Stop"

End Sub

'------------------------------------------------------------------------------------------------------------------
Private Sub CommandButton1_Click()

Dim H As Single
Dim M As Single
Dim S As Single
Dim Stp As Single
Start = Timer

Do
Stp = Timer - Start
H = Int(Stp / 3600)
M = Int((Stp - H * 3600) / 60)
S = Stp - H * 3600 - M * 60

Label1.Caption = S
Czas = Czas + S
Label2.Caption = Czas

DoEvents

Loop While Start > 0

End Sub

'------------------------------------------------------------------------------------------------------------------
Private Sub CommandButton2_Click()

Start = 0

End Sub

This is the stopwatch and in Label1 shows time between push START and STOP button
in Label2 shows sum of all START/STOP push (so it is sum all measurements) but results are fail...is same strange number
Please help me.

Benzadeus
09-27-2009, 06:22 AM
Option Explicit
Dim Start As Single
Dim Czas As Single
'------------------------------------------------------------------------------------------------------------------
Private Sub UserForm_Activate()

Label1.Caption = ""
Label2.Caption = ""
CommandButton1.Caption = "Start"
CommandButton2.Caption = "Stop"
End Sub
'------------------------------------------------------------------------------------------------------------------
Private Sub CommandButton1_Click()

Dim H As Single
Dim M As Single
Dim S As Single
Dim Stp As Single

Start = Timer

Do
Stp = Timer - Start
H = Int(Stp / 3600)
M = Int((Stp - H * 3600) / 60)
S = Stp - H * 3600 - M * 60

Label1 = S
Label2 = Czas + S
DoEvents
Loop While Start > 0
End Sub
'------------------------------------------------------------------------------------------------------------------
Private Sub CommandButton2_Click()
Czas = Label2
Start = 0
End Sub

Bob Phillips
09-27-2009, 07:32 AM
VBA has an integer division operator



H = Stp \ 3600