Dragonman86
New Member
- Joined
- Mar 16, 2016
- Messages
- 7
I have created a "Labor Sheet" to help track the labor hours, labor dollars, total time, strait time, and over time (among other things) of the employees who work under me. While setting up the spreadsheet, I wrote a rather large nested "IF" statement which rests within each cell because I am trying to break the hours worked down by shift. The "IF" statement is working the way it is supposed to and is as follows
In order to make it easier to update the entire spreadsheet, I have tried to put this "IF" statement into a UDF. I have the formula working for one of the inputs, but as soon as I try and have it calculate the next set of times, it returns an incorrect time. The following is my VBA:
The first sets of times (which are working) are "Start Time": 08:37, "End Time": 18:48, and "Shift Change": 18:37. The second set of times (which are not working) are "Start Time": 20:55, "End Time": 00:45, "Shift Change": 18:37.
The result for the second set of times which the Excel "IF" statement returns is 3.83, however the result for the VBA statement is -20.17.
I am not sure where, if at all, I have messed up and would appreciate any and all help
</w1d1shiftchange,w1d1actualend<w1d1shiftchange,w1d1actualstart></w1d1shiftchange,w1d1actualend></w1d1shiftchange,w1d1actualend<w1d1shiftchange,w1d1actualstart<0.58),
</w1d1shiftchange,w1d1actualend<0.58),
</w1d1shiftchange,w1d1actualend></w1d1shiftchange,w1d1actualend<w1d1shiftchange),
Code:
=IF(W1D1ActualStart>W1D1ShiftChange,IF(AND(W1D1ActualStart>W1D1ShiftChange,W1D1ActualEnd>W1D1ShiftChange),
IF(AND(W1D1ActualStart<w1d1shiftchange,w1d1actualend<w1d1shiftchange),
MOD(W1D1ActualEnd-W1D1ActualStart,1),
IF(AND(W1D1ActualStart<w1d1shiftchange,w1d1actualend>W1D1ShiftChange),
MOD(W1D1ShiftChange-W1D1ActualStart,1),
MOD(W1D1ActualEnd-W1D1ActualStart,1))),
IF(AND(W1D1ActualStart>W1D1ShiftChange,W1D1ActualEnd<w1d1shiftchange,w1d1actualend<0.58),
MOD(W1D1ActualEnd-W1D1ActualStart,1),
0)),
IF(AND(W1D1ActualStart<w1d1shiftchange,w1d1actualend<w1d1shiftchange,w1d1actualstart<0.58),
0,
IF(AND(W1D1ActualStart<w1d1shiftchange,w1d1actualend>W1D1ShiftChange),
MOD(W1D1ActualEnd-W1D1ShiftChange,1),
IF(AND(W1D1ActualStart<w1d1shiftchange,w1d1actualend<w1d1shiftchange,w1d1actualstart>0.58,W1D1ActualEnd<0.58),
MOD(W1D1ActualEnd-W1D1ShiftChange,1),
0))))*24
In order to make it easier to update the entire spreadsheet, I have tried to put this "IF" statement into a UDF. I have the formula working for one of the inputs, but as soon as I try and have it calculate the next set of times, it returns an incorrect time. The following is my VBA:
Code:
Function Shift2(StartTime, EndTime, ShiftChange)
If StartTime > ShiftChange Then
If StartTime > ShiftChange And EndTime > ShiftChange Then
If StartTime < ShiftChange And EndTime < ShiftChange Then
Shift2 = ((EndTime - StartTime) / 1) * 24
Else
If StartTime < ShiftChange And EndTime > ShiftChange Then
Shift2 = ((ShiftChange - StartTime) / 1) * 24
Else
Shift2 = ((EndTime - StartTime) / 1) * 24
End If
End If
Else
If StartTime > ShiftChange And EndTime < ShiftChange And EndTime < 0.58 Then
Shift2 = (EndTime - StartTime) * 24
Else
Shift2 = 0
End If
End If
Else
If StartTime < ShiftChange And EndTime < ShiftChange And StartTime < 0.58 Then
Shift2 = 0
Else
If StartTime < ShiftChange And EndTime > ShiftChang Then
Shift2 = ((EndTime - ShiftChange) / 1) * 24
Else
If StartTime < ShiftChange And EndTime < ShiftChange And StartTime > 0.58 And EndTime < 0.58 Then
Shift2 = ((EndTime - ShiftChange) / 1) * 24
Else
Shift2 = 0
End If
End If
End If
End If
End Function
The first sets of times (which are working) are "Start Time": 08:37, "End Time": 18:48, and "Shift Change": 18:37. The second set of times (which are not working) are "Start Time": 20:55, "End Time": 00:45, "Shift Change": 18:37.
The result for the second set of times which the Excel "IF" statement returns is 3.83, however the result for the VBA statement is -20.17.
I am not sure where, if at all, I have messed up and would appreciate any and all help
</w1d1shiftchange,w1d1actualend<w1d1shiftchange,w1d1actualstart></w1d1shiftchange,w1d1actualend></w1d1shiftchange,w1d1actualend<w1d1shiftchange,w1d1actualstart<0.58),
</w1d1shiftchange,w1d1actualend<0.58),
</w1d1shiftchange,w1d1actualend></w1d1shiftchange,w1d1actualend<w1d1shiftchange),