我有2个问题:
' Starting from first row, navigate through headers (rows) in lookup_range and return cell value at row_index if we reach a single column Range at the end of navigation; N/A otherwise.
Public Function HLOOKUPRANGE(headers() As Variant, lookup_range As range, row_index As Integer) As Variant
' returns the minimum value in search range that is greater than or equal to target.
Public Function MINGE(search As range, target As Variant) As Variant
在我的Excel工作表中,HLOOKUPRANGE的工作原理如下:
=HLOOKUPRANGE({"Color", "Red", "Alpha", 50}, A1:Z30, SELECTED_INDEX)
=HLOOKUPRANGE({"Color", "Blue", "Alpha", 100}, A1:Z30, SELECTED_INDEX)
但是我怎样才能让它与另一个函数一起工作呢?:
=HLOOKUPRANGE({"Color", "Red", "Alpha", MINGE(A4:Z4, INPUT_ALPHA_VALUE)}, A1:Z:30, SELECTED_INDEX)
Excel不允许我把MINGE放在{}之间,我认为问题在于以这种方式传递数组。如何将MINGE的结果传递给HLOOKUPRANGE?
提示:MINGE是“MINimum Greater than or Equal”
答案 0 :(得分:0)
我能想到的两种方式......
方式1
编写第3个函数,将Array与MINGE值联合起来。我创建了一个样本,因为我不知道HLOOKUPRANGE和MINGE做了什么,但概念是一样的。
在这个例子中,SID()模仿HLOOKUPRANGE。但它目前所做的只是对值进行求和
所以如果你输入
=SID({1,2,3,4,5})
它会给你总和15.现在让我们说我们想要总和
=SID({1,2,3,4,5,MINGE()})
其中MINGE()的值为6.这就是我们这样做的方式
=SID(Unite({1,2,3,4,5},6))
这将为您提供21.此代码位于模块
中Option Explicit
Dim TempAr() As Variant
Public Function Sid(headers() As Variant) As Variant
Dim temp As Long, i As Long
For i = LBound(headers) To UBound(headers)
temp = temp + headers(i)
Next i
Sid = temp
End Function
Public Function Unite(MyArray() As Variant, Vl As Variant) As Variant
Dim n As Long, i As Long
n = UBound(MyArray)
ReDim Preserve TempAr(n + 1)
For i = LBound(MyArray) To UBound(MyArray)
TempAr(i) = MyArray(i)
Next i
TempAr(n + 1) = Vl
Unite = TempAr
End Function
如果我将相同的内容应用到您的函数中,那么它看起来就像这样
=HLOOKUPRANGE(UNITE({"Color", "Red", "Alpha"},MINGE(A4:Z4, INPUT_ALPHA_VALUE)), A1:Z30, SELECTED_INDEX)
第2天
您可以稍微更改HLOOKUPRANGE函数以接受可选值。例如公式
= SUM({1,2,3,4,5},{6})在工作表中执行与上述相同的操作。
因此您可以将HLOOKUPRANGE()更改为
Public Function HLOOKUPRANGE(headers() As Variant, lookup_range As Range, _
row_index As Integer, Optional HeadersItem1 As Variant, _
Optional HeadersItem2 As Variant) As Variant
'~~> In the code add HeadersItem1 and HeadersItem2 to headers()
'~~> If they are not blank
End Function
USAGE就像这样
=HLOOKUPRANGE({"Color", "Red", "Alpha"}, A1:Z30, SELECTED_INDEX,MINGE(A4:Z4, INPUT_ALPHA_VALUE)))
或
=HLOOKUPRANGE({"Color", "Red"}, A1:Z30, SELECTED_INDEX,MINGE(A4:Z4, INPUT_ALPHA_VALUE)),SELECTED_INDEX,MINGE(B4:B4, INPUT_ALPHA_VALUE)))