=MATCH() equivalent for multidimensional ranges

前端 未结 1 1784
别那么骄傲
别那么骄傲 2021-01-16 05:27

I have an excel sheet, where cells A1-C20==INT(RAND()*10). This is my data range. Cell E1=1, E2=2, E3=3, etc. These are the values I am trying to find. I se

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 05:47

    You can use COUNTIF to do this since you only want to know whether the number is present (not its location).

    =COUNTIF(A:C,E1)>0
    

    This will return "TRUE" if it is present, "FALSE" if it is not.

    Just for fun, here's a worksheet function solution that returns the cell address that matches the lookup value. It uses the fact that you are only searching in 3 columns.

    =IF(ISERROR(MATCH(E1,A:A,0)),IF(ISERROR(MATCH(E1,B:B,0)),IF(ISERROR(MATCH(E1,C:C,0)),"Not found.","C"&MATCH(E1,C:C,0)),"B"&MATCH(E1,B:B,0)),"A"&MATCH(E1,A:A,0))
    

    I thought I'd also throw in a VBA solution that can return the match location inside a (contiguous) range. It looks at columns one at a time from left to right and returns the address of the first match found.

    Public Function MDMATCH(srchfor As String, lookin As Range) As String
    
    Application.Volatile
    Dim RngArray() As Variant
    Dim topleft As String
    Dim tmpval As String
    
    topleft = lookin.Address
    topleft = Left(topleft, InStr(topleft, ":") - 1)
    tmpval = "Not found."
    RngArray = lookin
    
    For i = 1 To UBound(RngArray, 2)
        If tmpval = "Not found." Then
            For j = 1 To UBound(RngArray, 1)
                If RngArray(j, i) = srchfor Then
                    tmpval = Range(topleft).Offset(j - 1, i - 1).Address
                    Exit For
                End If
            Next j
        Else
            Exit For
        End If
    Next i
    MDMATCH = tmpval
    End Function
    

    0 讨论(0)
提交回复
热议问题