udf使用sumproduct无法正常进行2d查找

时间:2014-11-14 01:39:21

标签: lookup

我正试图纠正一个2d udf。

= blookup(namedrange,hlookup,vlookup)

我写了以下但是没有用。请帮忙。

Function blookup(r, h, v)
Dim a, b, c, t, w
Dim r_count As Integer
Dim c_count As Integer
Dim x As Variant
r_count = r.Rows.Count
c_count = r.Columns.Count
t = r.Worksheet.name & "!"
a = r.Cells(2, 2).Address & ":" & r.Cells(r_count, c_count).Address
b = r.Cells(1, 2).Address & ":" & r.Cells(1, c_count).Address
c = r.Cells(2, 1).Address & ":" & r.Cells(r_count, 1).Address
x = Evaluate("=sumproduct(((" & t & b & ")=" & h & ")*((" & t & c & ")=" & v & ")*(" & t & a & "))")
blookup = x
End Function

2 个答案:

答案 0 :(得分:0)

现在看起来像是在工作,但我仍然怀疑它可能会破裂。

Function blookup(r As Range, h As String, v As String) As Variant
Dim a, b, c, t, w
Dim r_count As Integer
Dim c_count As Integer
Dim x As Variant
r_count = r.Rows.Count
c_count = r.Columns.Count
t = r.Worksheet.name & "!"
a = r.Cells(2, 2).Address & ":" & r.Cells(r_count, c_count).Address
b = r.Cells(1, 2).Address & ":" & r.Cells(1, c_count).Address
c = r.Cells(2, 1).Address & ":" & r.Cells(r_count, 1).Address
x = Evaluate("sumproduct(((" & t & b & ")=" & Chr(34) & h & Chr(34) & ")*((" & t & c & ")=" & Chr(34) & v & Chr(34) & ")*(" & t & a & "))")
blookup = x
End Function

答案 1 :(得分:0)

我意识到sumproduct不能用于非数字数据,所以我用vlookup和match编写了另一个版本。

Function blookup(r As Range, v As Variant, h As Variant) As Variant
Dim a As String, b As String, c As String, t As String, f As Variant, eh As String, ev As String
Dim r_count As Integer
Dim c_count As Integer
Dim x As String
r_count = r.Rows.Count
c_count = r.Columns.Count
If VarType(h) = vbDate Then eh = "Datevalue"
If VarType(v) = vbDate Then ev = "datevalue"
t = "'" & r.Worksheet.name & "'!"
a = r.Cells(2, 1).Address & ":" & r.Cells(r_count, c_count).Address
b = r.Cells(1, 1).Address & ":" & r.Cells(1, c_count).Address
x = "vlookup(" & ev & "(" & Chr(34) & v & Chr(34) & ")," & t & a & ", match(" & eh & "(" & Chr(34) & h & Chr(34) & ")," & t & b & ",0),false)"
f = Evaluate(x)
blookup = f
End Function