如何在Excel UDF中使用常量值?

我一直在努力想出在Excel中的UDF。 基本上我正在寻找的是一个函数,将需要2个input参数(常量)由用户提供,然后该函数将返回预先存储在Excel表中某个地方的值之一。 以下是它的样子:

月高峰closures

  1. 1月320 176
  2. 二月320 128
  3. 三月368 252
  4. 四月352 128
  5. 5月305 320

函数将如下所示:=小时(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(1月,峰值)时,我只从表中获取最后一个值,而不pipe参数是什么

考虑:

 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()语句以符合你的要求。