I assume the "on error goto 0" statement means that if the result (of the check against the holiday days) is an error then return zero?
No. It switches off the error handling. When you say "On Error Resume Next" you are using one of the resume options which is simply to carry on with the next statement as if the error didn't happen. You can then test the error condition, if you wish, and switch error trapping off, again, if you wish.
Problem was with the holiday lookup. Fixed.
Option Explicit
'function to calculate charges for employees
'using the employees name, the date on which work was carried out, and the hours worked as variables
Function MyPay(myname As String, mydate As Date, myhours As Variant)
Dim awf As WorksheetFunction: Set awf = WorksheetFunction
Dim Myday As Integer
Dim Normalhours As Double
Dim Overtime1hours As Double
Dim Overtime2hours As Double
Dim Hoursworked As Double
Dim Holidays As Range
Dim Normalrate As Double
Dim OT1rate As Double
Dim OT2rate As Double
' the intention is to lookup the employees nam in a range which is containes in the workbook called "Labour_Rates"
' column 1 contains the name, column 2 contains the basic rate, colunm 3, the first level overtime,
' and column 4 the upper level rate
Normalrate = awf.VLookup(myname, Range("Labour_Rates"), 2, False)
OT1rate = awf.VLookup(myname, Range("Labour_Rates"), 3, False)
OT2rate = awf.VLookup(myname, Range("Labour_Rates"), 4, False)
' there is also a named range called "Holidays",
' this contains bank holidays which carries the upper
' level rate if any work is carried out on that date.
Set Holidays = Range("Holidays")
' the charging structure depends on the weekday,
' thus the following intends to extract the date of
' the week from the variable "mydate"
Myday = Weekday(mydate)
' sometimes the cell containing the variable "myhours"
' is blank, I need these to calculate as zero's
If myhours = "" Then
Hoursworked = 0
Else: Hoursworked = CDbl(myhours)
End If
' this section details the charging structure.
' (myday 1) being a Sunday, on which al hours worked
' are charged at(OT2rate)
' the Dim of normalhours - OT1hours - OT2 Hours
' changes depending on the working day in the
' variable "Mydate"
' first though I need to decide if "mydate" is a
' holiday listed in the named range "Holidays"
Dim vHol As Integer
On Error Resume Next
vHol = 0: vHol = awf.Match(CLng(mydate), Holidays, 0)
On Error GoTo 0
'If awf.IsError(awf.Match(mydate, Holidays, 0)) Then
If vHol <> 0 Then
Normalhours = 0
Overtime1hours = 0
Overtime2hours = Hoursworked
ElseIf Myday = 1 Then
Normalhours = 0
Overtime1hours = 0
Overtime2hours = Hoursworked
ElseIf Myday >= 2 And Myday <= 5 Then
Normalhours = awf.Min(Hoursworked, 8)
Overtime1hours = awf.Max(0, Hoursworked - 8)
Overtime2hours = 0
ElseIf Myday = 6 Then
Normalhours = awf.Min(Hoursworked, 7)
Overtime1hours = awf.Max(0, Hoursworked - 7)
Overtime2hours = 0
ElseIf Myday = 7 Then
Normalhours = 0
Overtime1hours = awf.Min(Hoursworked, 5)
Overtime2hours = awf.Max(0, Hoursworked - 5)
End If
'Debug.Print mydate, Myday, Hoursworked, Normalhours, Overtime1hours, Overtime2hours
' then the bit that calculates ......
MyPay = (Normalhours * Normalrate) + _
(Overtime1hours * OT1rate) + _
(Overtime2hours * OT2rate)
End Function
If you fancy a challenge, would you know how to alter the code to make it work with an array of dates and times worked? so that the total could (for example) go in B10, and then I could do away with the total price for the day line altogether.
No, not me, I'm afraid. Could be just another function that loops through the array passing name, date and hours to the existing function. But, for simplicity, I think you'd need to repeat the name for each date/hours pair.
Regards, TMS
Bookmarks