Stop watch timer to capture accurate time of task completion

aliaslamy2k

Active Member
Joined
Sep 15, 2009
Messages
416
Office Version
  1. 2019
Platform
  1. Windows
I am looking for a VBA to know the accurate time taken to complete each task.

1.Column A 4– Contains Task Start date

2. From column B4 to K100 I will have data in each row

3. In Column L4 to L100 I need a stop watch timer to count the time and days taken to complete the tast (NOTE: Timer starts only when task date is entered in any column between A4 to A100)

4. If all columns are filled with data from in each row (i.e A4:K4) then I need a comment in M4 as “ Task Completed” and timer should stop. (Same should continue in all rows whenever task date is entered in Column A)

5. Once the time is captured, I want to protect the Task start date and timer cell to avoid manipulating by user. ( I should be able to enter my own password to unprotect).

I hope this is possible with excel VBA

Thank you
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
This can be done with the Worksheet_Change event handler for the sheet in question, e.g. "Sheet1".

Stopwatch timers aren't needed because the Worksheet_Change code appends the current time to the date entered by the user in A4:A100. With both the date and time, the code can calculate an accurate elapsed time in days, hours and minutes. You can format A4:A100 so that only the date is displayed or both the date and time are displayed.

Put this code in the ThisWorkbook module:
VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Worksheets("Sheet1").Protect Password:=ProtectSheetPassword, UserInterfaceOnly:=True
End Sub
Put this code in the Sheet1 module:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim AKcells As Range
    Dim DiffTime As Date

    If Not Intersect(Target, Range("A4:K100")) Is Nothing And Target.Cells.Count = 1 Then
    
        Application.EnableEvents = False
        
        If Target.Column = 1 Then
        
            'Append current time to date entered in column A cell
            
            If IsDate(Target.Value) Then
                Target.Value = Target.Value + Time
            End If
            
        Else
        
            Set AKcells = Range(Cells(Target.Row, "A"), Cells(Target.Row, "K"))
            
            'If all cells in columns A:K in the row are populated, put calculated Task Completion Time in L cell and "Task Completed" in M cell and lock A and L cells
            
            If WorksheetFunction.CountA(AKcells) = AKcells.Count Then
    
                DiffTime = Now - Cells(Target.Row, "A").Value                
                Cells(Target.Row, "L").Value = CLng(Int(DiffTime)) & " days " & Format(Hour(DiffTime), "00") & " hours " & Format(Minute(DiffTime), "00") & " mins"               
                Cells(Target.Row, "M").Value = "Task Completed"                
                Cells(Target.Row, "A").Locked = True
                Cells(Target.Row, "L").Locked = True
                Protect Password:=ProtectSheetPassword, UserInterfaceOnly:=True

            End If
        
        End If
        
        Application.EnableEvents = True
        
    End If
    
End Sub
Put this code in a standard module, e.g. Module1:
VBA Code:
Public Const ProtectSheetPassword As String = "xyz"

'Run this first to set all cells on active sheet to not locked

Public Sub Unlock_All_Cells()
    ActiveSheet.Cells.Locked = False
End Sub
You will see that the password to protect the sheet is "xyz". Change this as required.

Run the Unlock_All_Cells macro on the active sheet, then save, close and reopen the macro workbook.
 
Upvote 0

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

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