Time Tracker Code Clean-Up

Joined
Aug 15, 2019
Messages
12
Workbook: Time Tracker Workbook

The workbook has 45 different modules: 15 modules for Start/Resume, 15 modules for Stop & 15 modules for Reset. The only difference in the 'families' is the Row/Line they refer to (2 thru 16). Each module has been assigned to a 'button' on the corresponding Row/Line.

Start/Resume Module:
Sub Time_Sheet01_Start_Timer()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Time Sheet")
sh.Range("K2").Value = "Start"
If sh.Range("L2").Value = "" Then
sh.Range("L2").Value = Now
End If
x:
VBA.DoEvents
If sh.Range("K2").Value = "Stop" Then Exit Sub
sh.Range("M2").Value = Now
GoTo x
End Sub

Stop Module:
Sub Time_Sheet01_Stop_Timer()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Time Sheet")
sh.Range("K2").Value = "Stop"
Range("D2").Select
Selection.Copy
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("L2:M2").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A1").Select
End Sub

Reset Module:
Sub Time_Sheet01_Reset_Timer()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Time Sheet")
Range("E2").Select
ActiveCell.FormulaR1C1 = "0"
Range("A1").Select
End Sub

Just looking to see if there is a cleaner code.
 
i would imagine you do not need to be referring to Time Sheet in the code either, since all the buttons are probably on Time Sheet.

so the below would suffice and maybe easier to follow.

VBA Code:
Public RN As Integer
Sub get_row()
C = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Text
Set b = ActiveSheet.Shapes(Application.Caller)
RN = b.TopLeftCell.Row
If C = "START" Then Time_Sheet01_Start_Timer
If C = "STOP" Then Time_Sheet01_Stop_Timer
If C = "RESET" Then Time_Sheet01_Reset_Timer
If C = "RESUME" Then Time_Sheet01_Resume_Timer
End Sub
Sub Time_Sheet01_Start_Timer()
Range("K" & RN) = "Start"
If Range("L" & RN) = "" Then Range("L" & RN) = Now
Do Until Range("K" & RN) = "Stop"
    DoEvents
    Range("M" & RN) = Now
    If Range("K" & RN) = "Stop" Then Range("L" & RN & ":M" & RN).ClearContents
Loop
End Sub
Sub Time_Sheet01_Reset_Timer()
Range("E" & RN) = "0"
End Sub
Sub Time_Sheet01_Resume_Timer()
Range("K" & RN) = "Start"
If Range("L" & RN) = "" Then Range("L" & RN) = Now
Do Until Range("K" & RN) = "Stop"
    DoEvents
    Range("M" & RN) = Now
Loop
End Sub
Sub Time_Sheet01_Stop_Timer()
Range("K" & RN) = "Stop"
Range("E" & RN) = Format(Range("M" & RN) - Range("L" & RN), "HH:MM:SS")
End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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