Concat multiple MATCH criteria in Excel VBA

前端 未结 3 769
挽巷
挽巷 2020-12-18 15:06

So in Excel, we know it\'s possible to test against multiple criteria via concatenation, like this:

MATCH(criteria1&criteria2, Range(), 0)
3条回答
  •  忘掉有多难
    2020-12-18 15:23

    This works:

    Sub dural()
        crit1 = "find "
        crit2 = "happiness"
        N = Application.WorksheetFunction.Match(crit1 & crit2, Range("A1:A10"), 0)
        MsgBox N
    End Sub
    

    with, say A3 containing:

    find happiness

    EDIT#1:

    Consider the case of multiple criteria in several columns. For example:

    demo

    and we want VBA to retrieve the name of a small, black, dog

    without VBA in a worksheet cell we can use:

    =INDEX(D1:D100,SUMPRODUCT(--(A1:A100="dog")*(B1:B100="small")*(C1:C100="black")*ROW(1:100)),1)
    

    to get the name Oscar

    we can use the same formula in VBA

    Sub luxation()
        Dim s As String, s2 As String
        s = "INDEX(D1:D100,SUMPRODUCT(--(A1:A100=""dog"")*(B1:B100=""small"")*(C1:C100=""black"")*ROW(1:100)),1)"
        s2 = Evaluate(s)
        MsgBox s2
    End Sub
    

提交回复
热议问题