Prevent adding new time value

Hi Experts,

Please help. The attached script automatically updates the time sequence (add-in) at Column A based on total duration at Column D. E.g if cell A8 is 1:45 and Cell D8 is 0:30, therefore Cell A9 will be (A8+D8). But, the problem is the script automatically adds-in the time value in a new cell if the time doesn't exceed midnight (end of the day). E.g. Cell A28, time is 23:30, when I add total duration at column D (D28) with 1:00, the following cell has been automatically updated with “23:30”. The same situation happens in other cell during the midnight transition time (e.g. Cell A54, A75).

How to prevent the script not adding a new time value at and of the day if the total duration at column D not exceed “00:00”? Hope Experts could help. I have attached the workbook for Experts perusal.



Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Count > 1 Then
    For Each cel In Target.Cells
        Call Worksheet_Change(cel)
    Next cel
    Exit Sub
End If
If Target.HasFormula = True Then Exit Sub
If Target.Column = 1 Then
    If Target.Row < 9 Then Exit Sub
    If Target.Offset(-1, 0) = "" And Target.Offset(-3, 0) = "" Then
        Target.Offset(-4, 3).Value = 1 + Target.Value - Target.Offset(-4).Value
    ElseIf Target.Offset(-1, 0) <> "" Then
        Target.Offset(-1, 3).Value = Target.Value - Target.Offset(-1).Value
    End If
ElseIf Target.Column = 4 Then
    If Target.Value = "" Then
       ' Target.Offset(1, -3).ClearContents
    ElseIf Target.Value + Target.Offset(, -3).Value > 1 Then
        Target.Offset(4, -3).Value = Target.Value + Target.Offset(, -3) - 1
    Else
        Target.Offset(1, -3).Value = Target.Value + Target.Offset(, -3)
    End If
End If
Application.EnableEvents = True
End Sub

Open in new window

Time.xls
Billa7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael FowlerSolutions ConsultantCommented:
I think I understand what you are after

If a change is made to column d and the cell in column A on the next row is blank then do nothing.

If I have understood correctly then the attached will help.

If not - how do you determine that you are at the end of the day when the duration does not go past the end of the day?

Michael
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Count > 1 Then
    For Each cel In Target.Cells
        Call Worksheet_Change(cel)
    Next cel
    Exit Sub
End If
If Target.HasFormula = True Then Exit Sub
If Target.Column = 1 Then
    If Target.Row < 9 Then Exit Sub
    If Target.Offset(-1, 0) = "" And Target.Offset(-3, 0) = "" Then
        Target.Offset(-4, 3).Value = 1 + Target.Value - Target.Offset(-4).Value
    ElseIf Target.Offset(-1, 0) <> "" Then
        Target.Offset(-1, 3).Value = Target.Value - Target.Offset(-1).Value
    End If
ElseIf Target.Column = 4 Then
    If Target.Offset(1, -3).Value = "" Or Target.Value = "" Then
      Exit Sub
    ElseIf Target.Value + Target.Offset(, -3).Value > 1 Then
        Target.Offset(4, -3).Value = Target.Value + Target.Offset(, -3) - 1
    Else
        Target.Offset(1, -3).Value = Target.Value + Target.Offset(, -3)
    End If
End If
Application.EnableEvents = True
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Billa7Author Commented:
Hi Michael,

Thanks for the revised code, at least now its looks much more better. However, when I amend the duration, let say at cell C16, I modify total duration at cell D23, that will update cell A24 with a new time, but the changes in time only happen at this cell, Is that a possibilities to update the rest of the time sequence automatically for that day?
0
Billa7Author Commented:
Hi,

Thanks a lot for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.