Break down hours in specific time periods?

tr0lle

New Member
Joined
Mar 7, 2013
Messages
2
Hi all

At my work we have some different billing codes, each specifying an amount of money the client should pay.

The billing codes are as following:

Billing code 012: Week days 08.00-17.00
Billing code 013: Week days 17.00-08.00
Billing code 014: Weekends (SATURDAYS)
Billing code 015: Weekends (SUNDAYS and PUBLIC HOLIDAYS)

If I have two dates (start date and end date), let's say the start date is Friday 08-03-2013 00:00 and the end date is Monday 11-03-2013 12:00 - is it then possible to "break down" how many hours there is on each billing code? Resulting in something like this:

012: 13 hours
013: 23 hours
014: 24 hours
015: 24 hours

Thanks in advance for your time and help!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Paste code behind the sheet were the dates are entered. I tried a number of scenarios; this seems to be working.
Any validation out there?

Start date in A1
End date in B2

Billing Code (BC) 012 in B4
Billing Code (BC) 013 in B5
Billing Code (BC) 014 in B6
Billing Code (BC) 015 in B7

Option Explicit

Dim holidays(10) As Date

Sub setHolidays()
holidays(0) = #1/1/2013# 'New Year's Day
holidays(1) = #1/21/2013# 'Birthday of Martin Luther King, Jr.
holidays(2) = #2/18/2013# 'Washington's Birthday
holidays(3) = #5/27/2013# 'Memorial Day
holidays(4) = #7/4/2013# 'Independence Day
holidays(5) = #9/2/2013# 'Labor Day
holidays(6) = #10/14/2013# 'Columbus Day
holidays(7) = #11/11/2013# 'Veterans Day
holidays(8) = #11/28/2013# 'Thanksgiving Day
holidays(9) = #12/25/2013# 'Christmas Day
End Sub

Sub CalcBillingCodes()
Dim BC012 As Single
Dim BC013 As Single
Dim BC014 As Single
Dim BC015 As Single
Dim days As Single
Dim dates() As Date
Dim testDate As Date
Dim i As Integer
Dim wd As Integer
Dim uBnd As Integer
Dim isHolidaySun As Boolean
Dim holiday As Variant

setHolidays
days = (DateDiff("h", [A2].Value, [B2].Value)) / 24

'ensure start date/time is < end date/time
If days > 0 Then
ReDim dates(Int(days))
uBnd = UBound(dates)

'fill date array; keep partial of first and last days
For i = 0 To uBnd - 1
dates(i) = [A2].Value + i

If i <> 0 And i <> uBnd Then dates(i) = Int(dates(i))
Next
dates(uBnd) = [B2].Value

'parse each date in array
For i = 0 To uBnd
testDate = dates(i)
isHolidaySun = False

'if testDate is holiday or Sunday then 015
For Each holiday In holidays

'get weekday (Sun = 1,..., Sat = 7)
wd = Weekday(testDate, vbSunday)

'add hours
If Int(testDate) = holiday Or wd = vbSunday Then

'part of a day * hours in a day difference
BC015 = BC015 + (24 - (24 * (testDate - Int(testDate))))
isHolidaySun = True
Exit For
End If
Next

If Not isHolidaySun Then
'today hours
Dim startHour As Single
Dim endHour As Single
Dim todayHours As Single

'calc start and end hours for testDate
startHour = 0
endHour = 24

'start and end on same day
If days < 1 Then
startHour = Hour(dates(0))
endHour = Hour(dates(1))

'first day
ElseIf i = 0 Then
startHour = Hour(dates(i))

'last day
ElseIf i = UBound(dates) Then
endHour = Hour(dates(i))
End If

todayHours = endHour - startHour

'weekday-day then 012, weekday-night then 013
If wd > 1 And wd < 7 Then
'find days or nights
Dim nightHours As Single
nightHours = 0

nightHours = nightHours + IIf((8 - startHour) >= 0, 8 - startHour, 0)
nightHours = nightHours + IIf((endHour - 17) >= 0, endHour - 17, 0)
BC012 = BC012 + todayHours - nightHours
BC013 = BC013 + nightHours

'Saturday then 014
ElseIf wd = 7 Then
BC014 = BC014 + todayHours
End If
End If
Next
End If

[B4].Value = BC012
[b5].Value = BC013
[b6].Value = BC014
[b7].Value = BC015
End Sub
 
Upvote 0
Paste code behind the sheet were the dates are entered. I tried a number of scenarios; this seems to be working.
Any validation out there?

Start date in A1
End date in B2

Billing Code (BC) 012 in B4
Billing Code (BC) 013 in B5
Billing Code (BC) 014 in B6
Billing Code (BC) 015 in B7

Option Explicit

Dim holidays(10) As Date

Sub setHolidays()
holidays(0) = #1/1/2013# 'New Year's Day
holidays(1) = #1/21/2013# 'Birthday of Martin Luther King, Jr.
holidays(2) = #2/18/2013# 'Washington's Birthday
holidays(3) = #5/27/2013# 'Memorial Day
holidays(4) = #7/4/2013# 'Independence Day
holidays(5) = #9/2/2013# 'Labor Day
holidays(6) = #10/14/2013# 'Columbus Day
holidays(7) = #11/11/2013# 'Veterans Day
holidays(8) = #11/28/2013# 'Thanksgiving Day
holidays(9) = #12/25/2013# 'Christmas Day
End Sub

Sub CalcBillingCodes()
Dim BC012 As Single
Dim BC013 As Single
Dim BC014 As Single
Dim BC015 As Single
Dim days As Single
Dim dates() As Date
Dim testDate As Date
Dim i As Integer
Dim wd As Integer
Dim uBnd As Integer
Dim isHolidaySun As Boolean
Dim holiday As Variant

setHolidays
days = (DateDiff("h", [A2].Value, [B2].Value)) / 24

'ensure start date/time is < end date/time
If days > 0 Then
ReDim dates(Int(days))
uBnd = UBound(dates)

'fill date array; keep partial of first and last days
For i = 0 To uBnd - 1
dates(i) = [A2].Value + i

If i <> 0 And i <> uBnd Then dates(i) = Int(dates(i))
Next
dates(uBnd) = [B2].Value

'parse each date in array
For i = 0 To uBnd
testDate = dates(i)
isHolidaySun = False

'if testDate is holiday or Sunday then 015
For Each holiday In holidays

'get weekday (Sun = 1,..., Sat = 7)
wd = Weekday(testDate, vbSunday)

'add hours
If Int(testDate) = holiday Or wd = vbSunday Then

'part of a day * hours in a day difference
BC015 = BC015 + (24 - (24 * (testDate - Int(testDate))))
isHolidaySun = True
Exit For
End If
Next

If Not isHolidaySun Then
'today hours
Dim startHour As Single
Dim endHour As Single
Dim todayHours As Single

'calc start and end hours for testDate
startHour = 0
endHour = 24

'start and end on same day
If days < 1 Then
startHour = Hour(dates(0))
endHour = Hour(dates(1))

'first day
ElseIf i = 0 Then
startHour = Hour(dates(i))

'last day
ElseIf i = UBound(dates) Then
endHour = Hour(dates(i))
End If

todayHours = endHour - startHour

'weekday-day then 012, weekday-night then 013
If wd > 1 And wd < 7 Then
'find days or nights
Dim nightHours As Single
nightHours = 0

nightHours = nightHours + IIf((8 - startHour) >= 0, 8 - startHour, 0)
nightHours = nightHours + IIf((endHour - 17) >= 0, endHour - 17, 0)
BC012 = BC012 + todayHours - nightHours
BC013 = BC013 + nightHours

'Saturday then 014
ElseIf wd = 7 Then
BC014 = BC014 + todayHours
End If
End If
Next
End If

[B4].Value = BC012
[b5].Value = BC013
[b6].Value = BC014
[b7].Value = BC015
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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