UDFs for rotas

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
I've been trying to figure out something for work, initially to break down the component parts with formulas and then produce a result.

It finally dawned on me i might be better off writing a UDF for simplicity, and then I hit the snag I don't know enough about them to even start a small one (A good reference guide would be useful if you know of one)

The data



I need to look at the day and use that in the function, also the two percentage enteries, and for each cell take the string value, change to time values, so I can return a decimal hours between the highest / lowest (even across midnight)

I started with (which is rubbish)
Code:
Public Function Section2(rng As Range)
'Public Function Section2(start_time As String)
'Public Function Section2(DayValue As String, StartTime As String, EndTime As String)
'DayValue =
Dim Starttimetime As Date
Dim Endtimetime As Date
'Dim starttime
Dim endtime
start_time = Left(start_time, 4)
endtime = Right(endtime, 4)
Starttimetime = TimeValue(start_time)
Endtimetime = TimeValue(endtime)
End Function

can someone just fix the first part so that the values are drawn into the output cell, and I have day and percentages available in code to work from
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You're trying to return 30% and 60% of the time difference of each cell or each column?
 
Last edited:
Upvote 0
You're trying to return 30% and 60% of the time difference of each cell or each column?

First off, I just trying to return to the UDF A day, a left and right split of the time formatted as time. and the cells containing .3 and .6 so I can work with them in the UDF (Its going to get complex when rolled over night) and put a decimal time value back in the cell when its been processed. The longest rota is 43 weeks, If I can get the days to work appropriately then I stand some chance :)
 
Upvote 0
You can split the strings into an array and format them to time, then calculate the difference or multiply them by the percents
 
Upvote 0
You can split the strings into an array and format them to time, then calculate the difference or multiply them by the percents

Far more complex than that, yes the percentage will be applied to the whole hours. I don't know enough to just pick up the bits i want efficiently so i can take that forward
 
Upvote 0
This returned the day differences on your sample (easily converted to hours, and can include the percentages once I understand their purpose):


Excel 2010
ABCDEFG
1Band >4<30%60%
2MonTueWedThu
310630-18300630-18300630-18300630-18300.5
42NRDNRDNRDNRD
531800-0500NRDNRDNRD12
64NRD1800-05001800-05001800-0500
750715-19150715-19150715-19150715-1915
Sheet2
Cell Formulas
RangeFormula
G3=daterangeconv(C7)
G5=G3*24


Code:
Function daterangeconv(rng As Range)
Dim arr As Variant, t0 As Double, t1 As Double
arr = Split(rng, "-")
t0 = TimeValue(Format(arr(0), "0:00"))
t1 = TimeValue(Format(arr(1), "0:00"))
If t1 < t0 And t1 < 0.5 Then t1 = t1 + 1
daterangeconv = t1 - t0
End Function
 
Last edited:
Upvote 0
OK the rules
depending on staff grade in the NHS a different overtime enhancement is applied, the lower the grade the higher the uplift

Excel Workbook
ABC
2BandTime Plus
3150%100%
4244%88%
5337%74%
6430%60%
7530%60%
8630%60%
9730%60%
10830%60%
11930%60%
Bands Enhancements


8pm to 6am monday to friday is enhanced hours (first uplift), all day saturday and sunday moves to the next banding.

then the wrinkle, any weekday shift where more than 50% occurs after 8pm, has the pre eight pm start enhanced so 18:00 to 02:00 qualifies (75% after 20:00, but the enhancement is now paid from 18:00 rather than 20:00.

And the flip side is highest enhancement on a sunday that rolls into a monday night where the value drops, its going to take ages to do it all, I'm looking to rough out as much as possible, before looking for cases were it falls over.

I need to hand encode a complete set so i can then test against a calculted formula

and one other wrinkle, it has to roll sunday into monday, bearing in mind that the last row of the formula will have to then reflect from the first row on the Monday :)
 
Upvote 0
Is the table in post #1 for a single person? If so does that mean they had no weekends?
 
Last edited:
Upvote 0
24/7/365 its part of one rota of 43 weeks. would it help if i posted this rota in full. each person would work round this rota and repeat
 
Upvote 0
Excel Workbook
ABCDEFGH
4MonTueWedThuFriSatSun
510630-18300630-18300630-18300630-1830NRDNRDNRD
62NRDNRDNRDNRD1800-05001800-05001800-0500
731800-0500NRDNRDNRD0630-18300630-18300630-1830
84NRD1800-05001800-05001800-0500NRDNRDNRD
950715-19150715-19150715-19150715-1915NRDNRDNRD
106NRDNRDNRDNRD1915-07151915-07151915-0715
1171915-0715NRDNRDNRD0715-19150715-19150715-1915
128NRD1915-07151915-07151915-0715NRDNRDNRD
1390745-19000745-19000745-19000745-1900NRDNRDNRD
1410NRDNRDNRDNRD1900-06001900-06001900-0600
15111900-0600NRDNRDNRD0700-17000815-16000700-1500
1612NRD1900-06001900-06001900-0600NRDNRDNRD
17130730-16000730-16000730-16000730-1600NRDNRDNRD
1814NRDNRDNRDNRD0645-18450645-18450645-1845
19150645-1845NRDNRDNRD0730-16000900-17000900-1700
2016NRD0645-18450645-18450645-1845NRDNRDNRD
21170700-15450700-15450700-15450700-15450700-1545NRDNRD
22181600-02001600-02001600-02001600-0200NRDNRDNRD
23191100-20001100-20001545-0000NRDNRD0845-18000845-1800
24201545-00001545-0000NRDNRD1600-01001600-01001500-0000
2521NRDNRDNRD1545-00001545-01001700-01001700-0000
2622NRDNRD1100-20001100-20001100-2000NRDNRD
27231845-06451845-06451845-06451845-0645NRDNRDNRD
2824NRDNRDNRDNRD1845-06451845-06451845-0645
29251815-0615NRDNRDNRD1815-06151815-06151815-0615
3026NRD1815-06151815-06151815-0615NRDNRDNRD
31270700-18000700-1500NRDNRD1700-02001700-02001715-0200
3228NRDNRD1500-00001500-0000NRDNRDNRD
33291830-06301830-06301830-0630NRDNRDNRDNRD
34300600-18000600-18000600-1800NRDNRD0900-17000900-1900
35310700-1500NRDNRDNRD0615-18150615-18150615-1815
3632NRDNRDNRD1830-06301830-06301830-06301830-0630
3733NRDNRDNRD1800-05001800-0500NRDNRD
3834NRD0615-18150615-18150615-1815NRDNRDNRD
39351800-05001800-05001800-0500NRDNRDNRDNRD
40361500-00001500-0000NRDNRDNRD0600-18000600-1800
41370615-1815NRDNRDNRD0700-19000700-19000830-1715
4238NRDNRDNRD0730-18000730-18001800-05001800-0500
4339NRDNRD0730-18000600-18000600-1800NRDNRD
4440NRD0700-18000700-15000700-1500NRDNRDNRD
4541NRDNRDNRD0700-19001900-07001900-07001900-0700
4642NRDNRD1615-02001548-0200NRDNRDNRD
47431900-07001900-07001900-07001900-0700NRDNRDNRD
Expected



 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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