"If" Condition Triggering Inappropriately With Differences In Time Into The Next Day

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,562
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this snippit of code

Rich (BB code):
With ws_dsched
        .Unprotect
        For i = 6 To 47
            If IsNumeric(Left(.Cells(i, 8), 3)) Then
                sw = sw + 1
                stv = .Cells(i, 9)
                edv = .Cells(i, 10)
                If Not IsNumeric(stv) Then
                    .Cells(i, 9).Interior.Color = RGB(255, 0, 0)
                    ers = ers + 1
                End If
                If Not IsNumeric(edv) Then
                    .Cells(i, 10).Interior.Color = RGB(255, 0, 0)
                    ers = ers + 1
                End If
                If stv >= edv Then
                    .Cells(i, 9).Interior.Color = RGB(255, 0, 0)
                    ers = ers + 1
                End If
                Stop
                td = (edv - stv) * 24
                If td <> .Cells(i, 11) Then
                    .Cells(i, 11).Interior.Color = RGB(255, 0, 0)
                    ers = ers + 1
                End If
            Else
                snw = snw + 1
                'MsgBox "This is NOT a number."
            End If
        Next i
        If ers > 0 Then
            MsgBox "Please correct the highlighted errors."
            Exit Sub
        End If
End With

I am running into a issue with differences in times that cross over into the next day.
For example,
stv = 4:00 PM
edv = 0:00 AM

With this example, the code in blue will trigger as stv (start time) is greater than edv (end time), which yes is true, but not in this case. The shift is 8 hours from 4:00pm and carries into the next day. The code shouldn't trigger in this instance.

The value of edv is taken from a cell in the worksheet that has a formula of "=($J$11-$I$11+IF($J$11<$I$11,1,0))*24" which assists with user data entry error checking on the worksheet .

How can I overcome this while avoiding having to rewrite my code up to this point?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This is also causing problems with
Code:
td = (edv - stv) * 24
If td <> .Cells(i, 11) Then
    .Cells(i, 11).Interior.Color = RGB(255, 0, 0)
    ers = ers + 1
 End If

I substituted
Code:
td = (edv - stv) * 24
with
Code:
td = (edv - stv + (edv < stv)) * 24

But it's still flagging it as an error according to my criteria. The value of dt when stv = 4:00PM and edv = 0:00 AM is -40.
 
Last edited:
Upvote 0
Shouldn't the true/false outcome of (edv<stv) be added to edv only, in effect adding a day, prior to doing the subtraction ?
 
Upvote 0
Hello NoSparks. Thanks for chiming in.
To simply answer your question, I honestly don't know. I'm not certain I understand what your are suggesting. Sorry.
 
Upvote 0
Oh, sorry, that's what you actually had, but
TRUE is 1 in a formula, and -1 in VBA
so try
VBA Code:
td = (edv + Abs(edv < stv) - stv) * 24
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,949
Members
448,534
Latest member
benefuexx

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