Hours between two dates based on open hours in VBA/Excel
One of our KPIs is hours between arrival and departure, however the hours we are not working do not count against us. I am aware of networkdays and networkdays.intl and initially used it on one of our files
IF(IF(INDIRECT("$D"&ROW())<>"",ROUNDDOWN(IF(INDIRECT("$D"&ROW())>INDIRECT("$A"&ROW()),0,(NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)-NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$D"&ROW()),$W$2:$W$100)*MOD(INDIRECT("$D"&ROW()),1)-NETWORKDAYS(INDIRECT("$A"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)*(1-MOD(INDIRECT("$A"&ROW()),1)))*24),0),"")>24,"X","")
where $A is start time and $D is end time, and W2-W100 is holidays.
However this doesn't work because our "open" hours that count against us are Monday 6am to Friday 11pm and Saturday 7am to 2pm, but we have records that also cover the range of Friday 11pm to Saturday 7am that should show as zero, as well as potentially Saturday 2pm to 10pm. We also have holidays off however on those holidays third shift finishes at 6am that day and first starts at 6am the next, so some of the numbers get screwed up when using networkdays.
I've been trying off and on for months to figure this out and nothing is working.
Originally we had an access database that did as simple if week(start) > week(end) then time = end - start - 2. Obviously that doesn't fit our KPI requirement.
I also tried
Function customTime(startTime As Date, endTime As Date, holidays As Range) As Date
Dim returnTime
returnTime = endTime - startTime - (2 * (Application.WeekNum(endTime, 2) - Application.WeekNum(startTime, 2)))
If Application.Weekday(startTime, 2) = 6 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0)
If Application.Weekday(startTime, 2) = 7 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0) + 1
For Each hols In holidays
If startTime < hols And endTime > hols + 1 Then returnTime = returnTime - 1
Next hols
CustomTime = returnTime
End Function
which basically does the same thing as network days but accounts for work on the weekends (I think). I did eventually have to fix the new year bug.
returnTime = endTime - startTime - (2 * ((Year(endTime) * 52) + Application.WeekNum(endTime, 2) - (Year(startTime) * 52) - Application.WeekNum(startTime, 2)))
This was sufficient for a while but now we're looking to get more accurate data on these and want to exclude all of our exact hours. I've changed it to use open and close timestamps in two columns and in the process of adjusting the program to actually do the math here, I'm screwing up the syntax (I think) and causing it to error out and occasionally crash excel entirely.
Function CTime2(startTime As Date, endTime As Date, hoursrange As Range) As Date
Dim returnTime
returnTime = endTime - startTime
Dim hours As Variant
hours = hoursrange.Value
For i = LBound(hours, 1) + 1 To UBound(hours, 1)
If startTime < hours(i, 1) And endTime < hours(i, 1) Then
Exit For
ElseIf startTime > hours(i, 2) And endTime > hours(i, 2) Then
Next i
ElseIf startTime > hours(i, 1) And endTime < hours(i, 2) Then
Exit For
ElseIf startTime < hours(i - 1, 2) And endTime > hours(i, 1) Then
returnTime = returnTime - (hours(i, 1) - hours(i - 1, 1))
End If
Next i
CTime2 = returnTime
End Function
It's failed because it can't find a for loop for the exit or next, can't find an if loop for the else, can't find a do for a loop while (when I was using that as a continue). I don't know enough about vba to figure out what i'm doing wrong, or even if i'm on the right path here.
excel vba
New contributor
add a comment |
One of our KPIs is hours between arrival and departure, however the hours we are not working do not count against us. I am aware of networkdays and networkdays.intl and initially used it on one of our files
IF(IF(INDIRECT("$D"&ROW())<>"",ROUNDDOWN(IF(INDIRECT("$D"&ROW())>INDIRECT("$A"&ROW()),0,(NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)-NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$D"&ROW()),$W$2:$W$100)*MOD(INDIRECT("$D"&ROW()),1)-NETWORKDAYS(INDIRECT("$A"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)*(1-MOD(INDIRECT("$A"&ROW()),1)))*24),0),"")>24,"X","")
where $A is start time and $D is end time, and W2-W100 is holidays.
However this doesn't work because our "open" hours that count against us are Monday 6am to Friday 11pm and Saturday 7am to 2pm, but we have records that also cover the range of Friday 11pm to Saturday 7am that should show as zero, as well as potentially Saturday 2pm to 10pm. We also have holidays off however on those holidays third shift finishes at 6am that day and first starts at 6am the next, so some of the numbers get screwed up when using networkdays.
I've been trying off and on for months to figure this out and nothing is working.
Originally we had an access database that did as simple if week(start) > week(end) then time = end - start - 2. Obviously that doesn't fit our KPI requirement.
I also tried
Function customTime(startTime As Date, endTime As Date, holidays As Range) As Date
Dim returnTime
returnTime = endTime - startTime - (2 * (Application.WeekNum(endTime, 2) - Application.WeekNum(startTime, 2)))
If Application.Weekday(startTime, 2) = 6 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0)
If Application.Weekday(startTime, 2) = 7 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0) + 1
For Each hols In holidays
If startTime < hols And endTime > hols + 1 Then returnTime = returnTime - 1
Next hols
CustomTime = returnTime
End Function
which basically does the same thing as network days but accounts for work on the weekends (I think). I did eventually have to fix the new year bug.
returnTime = endTime - startTime - (2 * ((Year(endTime) * 52) + Application.WeekNum(endTime, 2) - (Year(startTime) * 52) - Application.WeekNum(startTime, 2)))
This was sufficient for a while but now we're looking to get more accurate data on these and want to exclude all of our exact hours. I've changed it to use open and close timestamps in two columns and in the process of adjusting the program to actually do the math here, I'm screwing up the syntax (I think) and causing it to error out and occasionally crash excel entirely.
Function CTime2(startTime As Date, endTime As Date, hoursrange As Range) As Date
Dim returnTime
returnTime = endTime - startTime
Dim hours As Variant
hours = hoursrange.Value
For i = LBound(hours, 1) + 1 To UBound(hours, 1)
If startTime < hours(i, 1) And endTime < hours(i, 1) Then
Exit For
ElseIf startTime > hours(i, 2) And endTime > hours(i, 2) Then
Next i
ElseIf startTime > hours(i, 1) And endTime < hours(i, 2) Then
Exit For
ElseIf startTime < hours(i - 1, 2) And endTime > hours(i, 1) Then
returnTime = returnTime - (hours(i, 1) - hours(i - 1, 1))
End If
Next i
CTime2 = returnTime
End Function
It's failed because it can't find a for loop for the exit or next, can't find an if loop for the else, can't find a do for a loop while (when I was using that as a continue). I don't know enough about vba to figure out what i'm doing wrong, or even if i'm on the right path here.
excel vba
New contributor
What do you want to do if the 2nd condition is true?Next i
is not appropriate there...
– BigBen
Jan 18 at 14:15
SOmethihg like this formula could be a start, it's weekdays * 6am to 23:00 and weekends 7am to 14:00. I may not have understood properly, just had a quick look=(NETWORKDAYS(K3,M3)*(24*(TIME(23,0,0)-TIME(6,0,0))))+INT((M3-K3-NETWORKDAYS(K3,M3))/2)*(24*(TIME(14,0,0)-TIME(7,0,0)))
K3 and M3 are my start and end dates (inclusive)
– Nathan_Sav
Jan 18 at 14:51
add a comment |
One of our KPIs is hours between arrival and departure, however the hours we are not working do not count against us. I am aware of networkdays and networkdays.intl and initially used it on one of our files
IF(IF(INDIRECT("$D"&ROW())<>"",ROUNDDOWN(IF(INDIRECT("$D"&ROW())>INDIRECT("$A"&ROW()),0,(NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)-NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$D"&ROW()),$W$2:$W$100)*MOD(INDIRECT("$D"&ROW()),1)-NETWORKDAYS(INDIRECT("$A"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)*(1-MOD(INDIRECT("$A"&ROW()),1)))*24),0),"")>24,"X","")
where $A is start time and $D is end time, and W2-W100 is holidays.
However this doesn't work because our "open" hours that count against us are Monday 6am to Friday 11pm and Saturday 7am to 2pm, but we have records that also cover the range of Friday 11pm to Saturday 7am that should show as zero, as well as potentially Saturday 2pm to 10pm. We also have holidays off however on those holidays third shift finishes at 6am that day and first starts at 6am the next, so some of the numbers get screwed up when using networkdays.
I've been trying off and on for months to figure this out and nothing is working.
Originally we had an access database that did as simple if week(start) > week(end) then time = end - start - 2. Obviously that doesn't fit our KPI requirement.
I also tried
Function customTime(startTime As Date, endTime As Date, holidays As Range) As Date
Dim returnTime
returnTime = endTime - startTime - (2 * (Application.WeekNum(endTime, 2) - Application.WeekNum(startTime, 2)))
If Application.Weekday(startTime, 2) = 6 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0)
If Application.Weekday(startTime, 2) = 7 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0) + 1
For Each hols In holidays
If startTime < hols And endTime > hols + 1 Then returnTime = returnTime - 1
Next hols
CustomTime = returnTime
End Function
which basically does the same thing as network days but accounts for work on the weekends (I think). I did eventually have to fix the new year bug.
returnTime = endTime - startTime - (2 * ((Year(endTime) * 52) + Application.WeekNum(endTime, 2) - (Year(startTime) * 52) - Application.WeekNum(startTime, 2)))
This was sufficient for a while but now we're looking to get more accurate data on these and want to exclude all of our exact hours. I've changed it to use open and close timestamps in two columns and in the process of adjusting the program to actually do the math here, I'm screwing up the syntax (I think) and causing it to error out and occasionally crash excel entirely.
Function CTime2(startTime As Date, endTime As Date, hoursrange As Range) As Date
Dim returnTime
returnTime = endTime - startTime
Dim hours As Variant
hours = hoursrange.Value
For i = LBound(hours, 1) + 1 To UBound(hours, 1)
If startTime < hours(i, 1) And endTime < hours(i, 1) Then
Exit For
ElseIf startTime > hours(i, 2) And endTime > hours(i, 2) Then
Next i
ElseIf startTime > hours(i, 1) And endTime < hours(i, 2) Then
Exit For
ElseIf startTime < hours(i - 1, 2) And endTime > hours(i, 1) Then
returnTime = returnTime - (hours(i, 1) - hours(i - 1, 1))
End If
Next i
CTime2 = returnTime
End Function
It's failed because it can't find a for loop for the exit or next, can't find an if loop for the else, can't find a do for a loop while (when I was using that as a continue). I don't know enough about vba to figure out what i'm doing wrong, or even if i'm on the right path here.
excel vba
New contributor
One of our KPIs is hours between arrival and departure, however the hours we are not working do not count against us. I am aware of networkdays and networkdays.intl and initially used it on one of our files
IF(IF(INDIRECT("$D"&ROW())<>"",ROUNDDOWN(IF(INDIRECT("$D"&ROW())>INDIRECT("$A"&ROW()),0,(NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)-NETWORKDAYS(INDIRECT("$D"&ROW()),INDIRECT("$D"&ROW()),$W$2:$W$100)*MOD(INDIRECT("$D"&ROW()),1)-NETWORKDAYS(INDIRECT("$A"&ROW()),INDIRECT("$A"&ROW()),$W$2:$W$100)*(1-MOD(INDIRECT("$A"&ROW()),1)))*24),0),"")>24,"X","")
where $A is start time and $D is end time, and W2-W100 is holidays.
However this doesn't work because our "open" hours that count against us are Monday 6am to Friday 11pm and Saturday 7am to 2pm, but we have records that also cover the range of Friday 11pm to Saturday 7am that should show as zero, as well as potentially Saturday 2pm to 10pm. We also have holidays off however on those holidays third shift finishes at 6am that day and first starts at 6am the next, so some of the numbers get screwed up when using networkdays.
I've been trying off and on for months to figure this out and nothing is working.
Originally we had an access database that did as simple if week(start) > week(end) then time = end - start - 2. Obviously that doesn't fit our KPI requirement.
I also tried
Function customTime(startTime As Date, endTime As Date, holidays As Range) As Date
Dim returnTime
returnTime = endTime - startTime - (2 * (Application.WeekNum(endTime, 2) - Application.WeekNum(startTime, 2)))
If Application.Weekday(startTime, 2) = 6 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0)
If Application.Weekday(startTime, 2) = 7 Then returnTime = returnTime + startTime - Application.RoundDown(startTime, 0) + 1
For Each hols In holidays
If startTime < hols And endTime > hols + 1 Then returnTime = returnTime - 1
Next hols
CustomTime = returnTime
End Function
which basically does the same thing as network days but accounts for work on the weekends (I think). I did eventually have to fix the new year bug.
returnTime = endTime - startTime - (2 * ((Year(endTime) * 52) + Application.WeekNum(endTime, 2) - (Year(startTime) * 52) - Application.WeekNum(startTime, 2)))
This was sufficient for a while but now we're looking to get more accurate data on these and want to exclude all of our exact hours. I've changed it to use open and close timestamps in two columns and in the process of adjusting the program to actually do the math here, I'm screwing up the syntax (I think) and causing it to error out and occasionally crash excel entirely.
Function CTime2(startTime As Date, endTime As Date, hoursrange As Range) As Date
Dim returnTime
returnTime = endTime - startTime
Dim hours As Variant
hours = hoursrange.Value
For i = LBound(hours, 1) + 1 To UBound(hours, 1)
If startTime < hours(i, 1) And endTime < hours(i, 1) Then
Exit For
ElseIf startTime > hours(i, 2) And endTime > hours(i, 2) Then
Next i
ElseIf startTime > hours(i, 1) And endTime < hours(i, 2) Then
Exit For
ElseIf startTime < hours(i - 1, 2) And endTime > hours(i, 1) Then
returnTime = returnTime - (hours(i, 1) - hours(i - 1, 1))
End If
Next i
CTime2 = returnTime
End Function
It's failed because it can't find a for loop for the exit or next, can't find an if loop for the else, can't find a do for a loop while (when I was using that as a continue). I don't know enough about vba to figure out what i'm doing wrong, or even if i'm on the right path here.
excel vba
excel vba
New contributor
New contributor
New contributor
asked Jan 18 at 14:11
BurmeindBurmeind
1
1
New contributor
New contributor
What do you want to do if the 2nd condition is true?Next i
is not appropriate there...
– BigBen
Jan 18 at 14:15
SOmethihg like this formula could be a start, it's weekdays * 6am to 23:00 and weekends 7am to 14:00. I may not have understood properly, just had a quick look=(NETWORKDAYS(K3,M3)*(24*(TIME(23,0,0)-TIME(6,0,0))))+INT((M3-K3-NETWORKDAYS(K3,M3))/2)*(24*(TIME(14,0,0)-TIME(7,0,0)))
K3 and M3 are my start and end dates (inclusive)
– Nathan_Sav
Jan 18 at 14:51
add a comment |
What do you want to do if the 2nd condition is true?Next i
is not appropriate there...
– BigBen
Jan 18 at 14:15
SOmethihg like this formula could be a start, it's weekdays * 6am to 23:00 and weekends 7am to 14:00. I may not have understood properly, just had a quick look=(NETWORKDAYS(K3,M3)*(24*(TIME(23,0,0)-TIME(6,0,0))))+INT((M3-K3-NETWORKDAYS(K3,M3))/2)*(24*(TIME(14,0,0)-TIME(7,0,0)))
K3 and M3 are my start and end dates (inclusive)
– Nathan_Sav
Jan 18 at 14:51
What do you want to do if the 2nd condition is true?
Next i
is not appropriate there...– BigBen
Jan 18 at 14:15
What do you want to do if the 2nd condition is true?
Next i
is not appropriate there...– BigBen
Jan 18 at 14:15
SOmethihg like this formula could be a start, it's weekdays * 6am to 23:00 and weekends 7am to 14:00. I may not have understood properly, just had a quick look
=(NETWORKDAYS(K3,M3)*(24*(TIME(23,0,0)-TIME(6,0,0))))+INT((M3-K3-NETWORKDAYS(K3,M3))/2)*(24*(TIME(14,0,0)-TIME(7,0,0)))
K3 and M3 are my start and end dates (inclusive)– Nathan_Sav
Jan 18 at 14:51
SOmethihg like this formula could be a start, it's weekdays * 6am to 23:00 and weekends 7am to 14:00. I may not have understood properly, just had a quick look
=(NETWORKDAYS(K3,M3)*(24*(TIME(23,0,0)-TIME(6,0,0))))+INT((M3-K3-NETWORKDAYS(K3,M3))/2)*(24*(TIME(14,0,0)-TIME(7,0,0)))
K3 and M3 are my start and end dates (inclusive)– Nathan_Sav
Jan 18 at 14:51
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Burmeind is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255734%2fhours-between-two-dates-based-on-open-hours-in-vba-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Burmeind is a new contributor. Be nice, and check out our Code of Conduct.
Burmeind is a new contributor. Be nice, and check out our Code of Conduct.
Burmeind is a new contributor. Be nice, and check out our Code of Conduct.
Burmeind is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255734%2fhours-between-two-dates-based-on-open-hours-in-vba-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you want to do if the 2nd condition is true?
Next i
is not appropriate there...– BigBen
Jan 18 at 14:15
SOmethihg like this formula could be a start, it's weekdays * 6am to 23:00 and weekends 7am to 14:00. I may not have understood properly, just had a quick look
=(NETWORKDAYS(K3,M3)*(24*(TIME(23,0,0)-TIME(6,0,0))))+INT((M3-K3-NETWORKDAYS(K3,M3))/2)*(24*(TIME(14,0,0)-TIME(7,0,0)))
K3 and M3 are my start and end dates (inclusive)– Nathan_Sav
Jan 18 at 14:51