Skip to content

Instantly share code, notes, and snippets.

@cwilper
Created April 15, 2017 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwilper/874d051e2b84f56f54a2e766145e5cb3 to your computer and use it in GitHub Desktop.
Save cwilper/874d051e2b84f56f54a2e766145e5cb3 to your computer and use it in GitHub Desktop.
Excel VBA code snippets
'If G and H are defined for the given row, add the difference to K and clear them
Private Sub Update_Total(ByVal Row As Integer)
Dim punchStart, punchEnd, timeWorked As Integer
punchStart = Range("G" & Row).Value
punchEnd = Range("H" & Row).Value
timeWorked = punchEnd - punchStart
If (punchStart > 0 And punchEnd > 0 And timeWorked > 0) Then
Range("K" & Row).Value = Range("K" & Row) + timeWorked
Range("G" & Row).Value = ""
Range("H" & Row).Value = ""
End If
End Sub
'Anytime a value in column 6 (F) changes, call Update_Total for that row
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 6) Then
Update_Total (Target.Row)
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment