1

我正在尝试编写一个自定义函数,它可以让我从满足 x 个条件的范围内的第一行中检索一个单元格。我想这将与 SUMIFS 的工作方式非常相似,只是更简单的是它不会在第一场比赛后继续处理。

有谁知道在 VBA 中重现 SUMIFS (excel 07) 函数的代码?

因此,例如,如果我在 excel 中有一个表格,例如:

W X Y Z
a b 6 1
a b 7 2
b b 7 3

我希望能够编写一个函数,它会给我列 Z 中的值,其中列 W=a,X=b,Y>=7(换句话说,值 2)。

假设我想要的记录是唯一的并且我希望返回一个数字,SUMIFS 可以大致做到这一点。不过,就我的目的而言,这些假设是行不通的。

4

3 回答 3

4

使用 ADO 的示例。

strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

'I want to be able to write a function that will give me the value '
'in column Z where columns W=a, X=b, Y>=7 '
'(in other words the value 2).' 

strSQL = "SELECT Top 1 Z " _
         & "FROM [Sheet1$] " _
         & "WHERE W='a' And X='b' And Y>=7"

rs.Open strSQL, cn

Result = rs.Fields("Z")
于 2009-01-09T15:49:30.623 回答
1

恕我直言,ADO 不适合在 excel 工作表函数中使用(性能差,不能轻易在包含数据的工作表上使用)。这是一个 VBA 替代方案:


Function MFind(theRange As Range, ParamArray Tests() As Variant) As Variant
'
' Parameters are:
' The Range to be searched
' the values to be searched for in successive columns
' all search values except the last use =
' the last search value uses >=
' the function returns the value from the last column in the range
'
    Dim vArr As Variant
    Dim j As Long
    Dim k As Long
    Dim nParams As Long
    Dim blFound As Boolean

vArr = theRange.Value2
nParams = UBound(Tests) - LBound(Tests) + 1
If nParams >= UBound(vArr, 2) Then
    MFind = CVErr(xlErrValue)
    Exit Function
End If

For j = 1 To UBound(vArr)
    blFound = True
    For k = LBound(Tests) To nParams - 2
        If vArr(j, k + 1) <> Tests(k) Then
            blFound = False
            Exit For
        End If
    Next k
    If blFound Then
        If vArr(j, nParams) >= Tests(nParams - 1) Then
            MFind = vArr(j, UBound(vArr, 2))
            Exit For
        End If
    End If
Next j

End Function

于 2009-01-11T13:37:09.827 回答
1

Deeno,为此使用 UDF 非常有用,但您也可以使用普通的 old =VLOOKUP()

VLOOKUP()只能通过查找一个“键”来工作,但您可以在左侧的帮助列中创建一个连接键。例如:

W X Y Z    AA
a b 6 ab6  1
a b 7 ab7  2
b b 7 bb7  3

然后=VLOOKUP(A1,$Z$1:$AA$3,2,FALSE),如果 A1 具有您正在寻找的价值。如果您的数据更复杂,您可以使用未使用的字符(例如:管道)连接数据,这样您就有 a|B|6 而不是 ab6。

于 2009-12-20T08:04:49.003 回答