Hi all,
I've found some code which i'm using for a staff scheduling system. It can turn strings such as 12pm-5pm into time and then work out the hours which is great, it will also add them up for any given working week. However, what I really need is some more code that will count down the rows rather than the columns and give me a total employee count per hour.
For instance, I have 3 employees - A, B, and C.
What I'd like to be able to do for any given day is have a count for how many hours are being spent, per hour. So using the above table it would count 1 hour for 11am-12pm, 3 hours from 12pm -1pm, etc.
Name Monday A 11am - 3pm B 12pm - 6pm C 12pm - 9pm
Please see the current code below, if you can see a way I can extrapolate the data I need from the data already created then I'd be really grateful, my VBA skills are very limited as I only delve into it when standard formulas won't cut it. I appreciate any help that you guys and gals can give.
Thank you in advance!![]()
Function Hours(rng As Range) Hours = GetCellHours(rng) End Function Function Pay(rng As Range) Pay = rng(3) * rng(5) End Function Function GetCellHours(rng As Range) Dim cell As Range Dim Hours As Double For Each cell In rng If cell.Column >= 7 Then If cell.Text <> "" Then Dim lines() As String Dim line lines = Split(Replace(cell.Text, "/", vbLf), vbLf) For Each line In lines Hours = Hours + GetLineHours(CStr(line)) Next line Else If rng.Worksheet.Cells(5, cell.Column).Text = "" Then GoTo Finish End If End If End If Next cell Finish: If Hours < 0 Then Hours = 0 GetCellHours = Hours End Function Function GetLineHours(str As String) Dim Hours As Double On Error GoTo NoHours If str = Null Then GoTo NoHours If str = "" Then GoTo NoHours str = Trim(str) If str = "" Then GoTo NoHours Dim words() As String If InStr(str, "-") Then words = Split(str, "-") If UBound(words) + 1 = 2 Then Dim start As Date start = CDate(words(0)) Dim Finish As Date Finish = CDate(words(1)) Hours = 24 * (Finish - start) If Hours <= 0 Then Hours = Hours + 24 End If End If ElseIf InStr(str, " ") Then words = Split(str) If UBound(words) + 1 = 3 Then If words(2) = "break" Then Dim amount As Double amount = CDbl(words(0)) Dim interval As String interval = words(1) If interval = "minute" Or interval = "min" Or interval = "mins" Then amount = (amount / 60) ElseIf interval = "hours" Or interval = "hour" Or interval = "hr" Or interval = "hrs" Then Else amount = 0 End If Hours = -amount End If End If If UBound(words) + 1 = 2 Then If words(1) = "hours" Or words(1) = "hour" Then Hours = CDbl(words(0)) End If End If End If GoTo NoError NoHours: Hours = 0 NoError: GetLineHours = Hours End Function
~Wabbit
Bookmarks