我一直在努力想在Excel中提出UDF。基本上我正在寻找的是一个函数,它将获取用户提供的2个输入参数(常量),然后该函数将返回预先存储在某个excel表中的值之一。这就是它的样子:
功能如下:=小时(2月,峰值)然后它将返回2月高峰时段,依此类推。
我感谢所有的帮助。非常感谢
编辑:代码已添加为评论
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
当我尝试在= calculateHours(january,peak)中使用此函数时,无论参数是什么,我只从表中获取最后一个值
答案 0 :(得分:2)
考虑:
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
当然,您可以修改 Array()语句以符合您的要求。