VBA code: calculating time difference
Closed
vathsala
-
Updated on Jan 10, 2022 at 12:04 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Contributor Last seen December 27, 2022 - Nov 4, 2021 at 12:37 PM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Contributor Last seen December 27, 2022 - Nov 4, 2021 at 12:37 PM
Related:
- Vba timediff
- Vba case like - Guide
- Excel online vba - Guide
- Vba timer - Guide
- Vba excel mac - Guide
- Vba color index - Guide
1 response
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Contributor
Last seen
December 27, 2022
555
Nov 4, 2021 at 12:37 PM
Nov 4, 2021 at 12:37 PM
Hi Vathsala,
Not sure how your sheet is made up, but when you have times like "hh:mm:ss" in column A and you want the time difference in column D, then you can give the code below a try:
Best regards,
Trowa
Not sure how your sheet is made up, but when you have times like "hh:mm:ss" in column A and you want the time difference in column D, then you can give the code below a try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Columns("A")) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
With Target.Offset(0, 3)
.NumberFormat = "hh:mm:ss"
.Value = Target.Offset(1, 0).Value - Target.Value
If .Row = 2 Then Exit Sub
If .Value < 0 Then .Value = vbNullString
End With
With Target.Offset(-1, 3)
.NumberFormat = "hh:mm:ss"
.Value = Target.Value - Target.Offset(-1, 0).Value
End With
End Sub
Best regards,
Trowa