I've been struggling to come up with a UDF in Excel. basically what I'm looking for is a function that would take 2 input parameters(constant) provided by a user, then the function would return one of the values pre-stored in an excel table somewhere. Here's what it would look like:
The function would be like this: =Hours(February, Peak) then it would return February peak hours and so on.
I appreciate all the help. Thanks a lot
EDIT: Code added as comment
Public Function calculateHours(ByVal mo As Variant, ByVal period As Variant) As Integer
Dim tablette As Range
Set tablette = Worksheets("Hours").Range("U4:V15")
Set valueRange = Worksheets("Hours").Range("U4:U15")
period = "peak"
For Each ref In valueRange
mo = WorksheetFunction.VLookup(ref, tablette, 1, 0)
Next
ref calculateHours = WorksheetFunction.VLookup(mo, tablette, 2, False)
End Function
When I try to use this function such in =calculateHours(january, peak) I only get the last value from table regardless what the parameters are
Consider:
Public Function ReturnVale(period As String, MinMax As String) As Variant
months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
Off = Array(330, 347, 366, 393, 307, 353, 310, 347, 332, 307, 400, 333)
Peak = Array(399, 428, 440, 446, 366, 431, 370, 413, 389, 364, 470, 421)
For i = LBound(months) To UBound(months)
If period = months(i) Then
GoTo done
End If
Next i
ReturnVale = "NOT FOUND"
Exit Function
done:
If MinMax = "Peak" Then
ReturnVale = Peak(i)
Else
ReturnVale = Off(i)
End If
End Function
Naturally you would modify the Array() statements to match your requirements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.