Problem With Timestamp past 12 midnight to the next day.

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
966
Office Version
  1. 2016
Platform
  1. Windows
Hello All,
I'm having an issue with my 2nd shift time of 4:00pm to 3:00am the next day. The problem occurs, when I cross over midnight, and the date has changed.
It's beginning to annoy me.

VBA Code:
'Add 1st Start/End
Range("A1").Select
    ActiveCell.FormulaR1C1 = "1st Shift Start"
Range("B1").Select
    ActiveCell.FormulaR1C1 = "1st Shift End"
Range("A2").Select
    Selection.NumberFormat = "h:mm:ss"
    ActiveCell.FormulaR1C1 = "5:00:00 AM"     '1st Shift Start
Range("B2").Select
    Selection.NumberFormat = "h:mm:ss"
    ActiveCell.FormulaR1C1 = "16:00:00 PM"     '1st Shift End

'Add 2nd Shift Start/End
Range("D1").Select
    ActiveCell.FormulaR1C1 = "2nd Shift Start"
Range("E1").Select
    ActiveCell.FormulaR1C1 = "2nd Shift End"
Range("D2").Select
    Selection.NumberFormat = "h:mm:ss"
    ActiveCell.FormulaR1C1 = "16:30:00 PM"     '2nd Shift Start
Range("E2").Select
    Selection.NumberFormat = "[h]:mm:ss"
    ActiveCell.FormulaR1C1 = "28:00:00 AM"      '2nd Shift End
    

'Add 1st Shift Actual Wt.
Range("N3").Select

    ActiveCell.FormulaR1C1 = "1st Shift Actual Wt."
     Range("N4").Select
        ActiveCell.FormulaR1C1 = "=IF(AND(RC2>=R2C1,RC2<=R2C2),RC7,"""")"
        Selection.AutoFill Destination:=Range("N4:N" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
    Columns("N:N").NumberFormat = "0.00"

    
    
'Add 2nd Shift Actual Wt.
Range("O3").Select

    ActiveCell.FormulaR1C1 = "2nd Shift Actual Wt."
     Range("O4").Select
        ActiveCell.FormulaR1C1 = "=IF(AND(RC2>=R2C4,RC2<=R2C5),RC7,"""")"
        Selection.AutoFill Destination:=Range("O4:O" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
    Columns("O:O").NumberFormat = "0.00"

My 1st shift code works fine, because it stays on the same date.
The 2nd shift code works fine, until it crosses over to the next day, and I'm not really seeing why. For example 01:00:00 <= 03:00:00. however it is not picking it up, because it moves to the next date range.
I used 28:00:00 to try and trick it to think that is 4:00am...it didn't work
How do I get around this? Is there a TimeSerial I can use?
Thanks for the the help
 
@Guzzlr
I find your explanation of things a little confusing.
With the picture in post 10,
if it is reasonable to think that all the times in column C are end times
and the time in E3 is to be the start time for all of them
then I would try this. It puts the shift start and end in E3 and F3
and the formula for hours worked in column E from row 5 down
VBA Code:
Sub RunProgram()

Sheets("VBACodeWay").Select

' Add shift start info
Range("E2").Value = "Start"
Range("E3").Value = TimeSerial(17, 0, 0)

' Add shift end info
Range("F2").Value = "End"
Range("F3").Value = TimeSerial(4, 0, 0)

' Add the hours worked formula where required
With Range("E5:E" & Range("B" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(OR(R3C5="""",RC3=""""),""--"",(RC3-R3C5+(RC3<R3C5))*24)"
    .NumberFormat = "0.00"
End With

' Position the cursor
Range("A1").Select

End Sub

If that's wrong or you still don't follow then you should consider sharing a desensitized workbook of what you are actually working with.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
until now we only used the time, not the date.
In E3, you also have no date included.
I used E3 as starttime and the C-column as endtime

VBA Code:
Range(Range("E5"), Range("E" & Range("C" & Rows.Count).End(xlUp).Row)).FormulaR1C1 = "=IF(COUNT(R3C5,RC3)=2,+RC3-R3C5+(RC3<R3C5),""-"")"
this is a more condensed way of writing, but does the same.
The part before the equation-sign deteminates the range, starting in E5 and goes to the E-cell of the last used row of C, the 2nd part is the formula i made for E5, with the right $-signs,but now for all the rest

Cell Formulas
RangeFormula
C6:C19C6=+C5+TIME(0,30,0)
E4:E19E4=IF(COUNT($E$3,$C4)=2,(+$C4-$E$3+($C4<$E$3)),"-")
 
Upvote 0
@Guzzlr
I find your explanation of things a little confusing.
With the picture in post 10,
if it is reasonable to think that all the times in column C are end times
and the time in E3 is to be the start time for all of them
then I would try this. It puts the shift start and end in E3 and F3
and the formula for hours worked in column E from row 5 down
VBA Code:
Sub RunProgram()

Sheets("VBACodeWay").Select

' Add shift start info
Range("E2").Value = "Start"
Range("E3").Value = TimeSerial(17, 0, 0)

' Add shift end info
Range("F2").Value = "End"
Range("F3").Value = TimeSerial(4, 0, 0)

' Add the hours worked formula where required
With Range("E5:E" & Range("B" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(OR(R3C5="""",RC3=""""),""--"",(RC3-R3C5+(RC3<R3C5))*24)"
    .NumberFormat = "0.00"
End With

' Position the cursor
Range("A1").Select

End Sub

If that's wrong or you still don't follow then you should consider sharing a desensitized workbook of what you are actually working with.
So this I tried (code above), and unfortunately it filled all of column E.
is it missing:
Rich (BB code):
ActiveCell.Formula = "=IF(AND($C5>=$E$3,$C5<=$F$3),$D5,"""")"

What I'm trying to accomplish, is if the times in Column C, fall between the range of start and finish (E3 and F3), then the number associated in column D, will install in Column E.
I can get it to work, up until midnight, when it crosses into the next day, then the program will no longer work. For example, Row 27 and 28, crosses over midnight to the next day. My program: 'ActiveCell.Formula = "=IF(AND($C5>=$E$3,$C5<=$F$3),$D5,"""")" will place the number in column D into column E, up to row 27, and no further.
Thanks for the help

1642975416392.png
 
Last edited:
Upvote 0


Here is a link to the file, I've never tried this before, so not sure if it will work.
The top file is my code and you can see it works, up unill midnight, then no longer works as it moves to the next day
The second file is with NoSparks code.
The two can be compared.
Please keep in mind, this is a small test file, with nicely spaced time stamps, in real life, the file will have many time stamps in the seconds range, different from the previous time stamp.
However, there will never be a duplicate time stamp on the same day.
My thinking is if code will work on this small file, it should also work on the "real file"
 
Last edited:
Upvote 0
Map1
DEFGH
15start
1617:0021:00
17timenumber
180:00A-
191:00A-
202:00A-
213:00A-
224:00A-
235:00A-
246:00A-
257:00A-
268:00A-
279:00A-
2810:00A-
2911:00A-
3012:00A-
3113:00A-
3214:00A-
3315:00A-
3416:00A-
3517:00AA
3618:00AA
3719:00AA
3820:00AA
3921:00AA
4022:00A-
4123:00A-
420:00A-
43
Blad1
Cell Formulas
RangeFormula
D19:D42D19=+D18+TIME(1,0,0)
F18:F42F18=IF(OR($F$16=D18,$G$16=D18),E18, IF(MEDIAN($F$16,$G$16,D18)=D18,IF($F$16<$G$16,E18,"--"),IF($G$16<$F$16,E18,"-")))


variant
Map1
DEFG
14
15start
1617:0015:00
17timenumber
180:00AA
191:00AA
Blad1
Cell Formulas
RangeFormula
D19D19=+D18+TIME(1,0,0)
F18:F19F18=IF(($F$16=D18)+($G$16=D18)+(MEDIAN($F$16,$G$16,D18)=D18)*($F$16<$G$16)+(MEDIAN($F$16,$G$16,D18)<>D18)*($G$16<$F$16),E18,"-")
 
Last edited:
Upvote 0
In post 7 (?) there are dates in one column and times in the next. I have to wonder what's wrong with what I suggested; i.e. why you cannot use DateDiff as long as you can string together those 2 columns as 1 date/time value? So comparing the 1st two rows:
Datediff("n",#1/3/2022 9:44:20 PM#,#1/4/2022 12:35:25 AM#)/60 = 2.85 hours. Perhaps you want to see hours and minutes, but that's another step.

Perhaps the end goal escapes me, but from what I recall reading here, the thread is about calculating the time span over midnight and not too much about what to do with the calculation.
 
Upvote 0
Micron's idea is even easier
Map2
ABCDE
1start
223/01/2022 17:0024/01/2022 15:31
3timenumber
424/01/20220:00:00AA
524/01/20220:30:00AA
624/01/20221:00:00AA
724/01/20221:30:00AA
824/01/20222:00:00AA
924/01/20222:30:00AA
1024/01/20223:00:00AA
1124/01/20223:30:00AA
1224/01/20224:00:00AA
1324/01/20224:30:00AA
1424/01/20225:00:00AA
1524/01/20225:30:00AA
1624/01/20226:00:00AA
1724/01/20226:30:00AA
1824/01/20227:00:00AA
1924/01/20227:30:00AA
2024/01/20228:00:00AA
2124/01/20228:30:00AA
2224/01/20229:00:00AA
2324/01/20229:30:00AA
2424/01/202210:00:00AA
2524/01/202210:30:00AA
2624/01/202211:00:00AA
2724/01/202211:30:00AA
2824/01/202212:00:00AA
2924/01/202212:30:00AA
3024/01/202213:00:00AA
3124/01/202213:30:00AA
3224/01/202214:00:00AA
3324/01/202214:30:00AA
3424/01/202215:00:00AA
3524/01/202215:30:00AA
3624/01/202216:00:00A-
3724/01/202216:30:00A-
3824/01/202217:00:00A-
3924/01/202217:30:00A-
Blad1
Cell Formulas
RangeFormula
B5:B39B5=+B4+TIME(0,30,0)
D4:D39D4=IF(MEDIAN($D$2,$E$2,A4+B4)=A4+B4,C4,"-")
 
Upvote 0
Solution
For BSALV post
In post 7 (?) there are dates in one column and times in the next. I have to wonder what's wrong with what I suggested; i.e. why you cannot use DateDiff as long as you can string together those 2 columns as 1 date/time value? So comparing the 1st two rows:
Datediff("n",#1/3/2022 9:44:20 PM#,#1/4/2022 12:35:25 AM#)/60 = 2.85 hours. Perhaps you want to see hours and minutes, but that's another step.

Perhaps the end goal escapes me, but from what I recall reading here, the thread is about calculating the time span over midnight and not too much about what to do with the calculation.
So post 7 is the full dat set, with rows down to about 4000+ this early in the year.
Columns A and Column B exhibit a Date and Time, column G exhibits a number.
if the Time in Column B is between a range of 5:00:00PM and 04:00:00AM, then the associated number in Column G will be reported,
the link Is basically the same thing, only with out all the columns.
Sorry for any confusion
Thank you for your time
 
Upvote 0
To BSALV formulas in post 17, they are working!
However, I am not sure if it will work with times that have seconds and no longer in an orderly fashion every 30 minutes.
The real data set, as in post 7, have multiple time spans, and the only thing good is the descend in an orderly fashion based upon the date.


I modified the code a little to correspond with start and finish times
Rich (BB code):
=IF(OR($F$3=$D5,$G$3=$D5),$E5, IF(MEDIAN($F$3,$G$3,D5)=D5,IF($F$3<$G$3,E5,"--"),IF($G$3<$F$3,E5,"-")))

1642979967645.png
 
Last edited:
Upvote 0
i'll work with a precision of 1 second and for every possible time in D
 
Upvote 0

Forum statistics

Threads
1,215,733
Messages
6,126,541
Members
449,316
Latest member
sravya

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