Excel Macro recording elapsed time - Help please!!

Chrisjd2

Board Regular
Joined
Mar 1, 2016
Messages
61
Hi All,

I am fairly new to excel VBA, I need a macro that helps log downtime on a system.

I propose to do this via a button that starts and stops a timer, in reality the first click - takes a time stamp, and so doesnt the second. Then you minus the two, giving you the allotted time.

However, because I need it to accumulate per day (on 1 row) I cannot get it to work like I want it to.

Could someone have a look please, I have posted the code and I will show you how the spreadsheet looks like.

Regards
Chris

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Day[/TD]
[TD]Save Time[/TD]
[TD]View Time[/TD]
[TD]Lost Time[/TD]
[TD]Total (hrs)[/TD]
[TD]Total (dec)[/TD]
[TD]Job No[/TD]
[TD]Comments[/TD]
[TD]H (column)[/TD]
[TD]I (column)[/TD]
[TD]J (column)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]



Code:
Sub DownTime(xTime As Integer)

Dim COUNTER As Integer
Dim wsdowntime As Worksheet
Dim StartTime As String
Dim EndTime As String


Set wsdowntime = Worksheets("Downtime")




COUNTER = 2


While wsdowntime.Cells(COUNTER, 1) <> ""
COUNTER = COUNTER + 1
Wend


cEnd = COUNTER


COUNTER = 3




While wsdowntime.Cells(COUNTER, 1) <> ""


    If wsdowntime.Cells(COUNTER, 1) = Date Then
        If wsdowntime.Cells(COUNTER, 9) <> "" Then
            If wsdowntime.Cells(COUNTER, 2) = "" Then
                wsdowntime.Cells(COUNTER, 10) = Time
                wsdowntime.Cells(COUNTER, xTime) = (wsdowntime.Cells(COUNTER, 10) - wsdowntime.Cells(COUNTER, 9)) + wsdowntime.Cells(COUNTER, xTime)
            End If
        End If
    End If


    If wsdowntime.Cells(COUNTER, 1) = Date Then
        If wsdowntime.Cells(COUNTER, 9) = "" Then
            If wsdowntime.Cells(COUNTER, 2) <> "" Then
                wsdowntime.Cells(COUNTER, 9) = Time
            End If
        End If
    End If


    If wsdowntime.Cells(COUNTER, 1) = Date Then
        If wsdowntime.Cells(COUNTER, 9) <> "" Then
            If wsdowntime.Cells(COUNTER, 2) <> "" Then
                wsdowntime.Cells(COUNTER, 10) = Time
                wsdowntime.Cells(COUNTER, xTime) = (wsdowntime.Cells(COUNTER, 10) - wsdowntime.Cells(COUNTER, 9)) + wsdowntime.Cells(COUNTER, xTime)
                clear9 = 1
                clear10 = 1
            End If
        End If
    End If
 


COUNTER = COUNTER + 1


Wend






COUNTER = 3
While COUNTER <= cEnd


If wsdowntime.Cells(COUNTER, 1) = Date Then
    newline = 1
End If


COUNTER = COUNTER + 1
Wend


If newline = "" Then
    wsdowntime.Cells(COUNTER - 1, 1) = Date
    wsdowntime.Cells(COUNTER - 1, 9) = Time
End If


End Sub


Sub cSaving()
    Call DownTime(2)
End Sub


Sub cViews()
    Call DownTime(3)
End Sub


Sub cLostWork()
    Call DownTime(4)
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top