2011-08-24 98 views
1

我有一個Excel工作表,其中單元格A1-C20 = =INT(RAND()*10)。這是我的數據範圍。單元格E1 = 1,E2 = 2,E3 = 3等。這些是我試圖找到的值。我設置小區F1 = =MATCH(E1,A:C,0),F2 = =MATCH(E1,A:C,0)=匹配()相當於多維範圍

然而,所有的MATCH函數返回#N/A,因爲輸入範圍是多維的。如何測試給定值(1,2,3,4等)是否存在於多維範圍(A1-C20)中?

/編輯:This function工程,但是比我需要更多。有沒有什麼辦法讓它只返回TRUE或FALSE,取決於查找值是否在範圍內?

Function OzgridLookup(Find_Val As Variant, Occurrence As Long, Table_Range As Range, _ 
Offset_Cols As Long, Optional Column_Lookin As Long, Optional Row_Offset As Long) As Variant 

Dim lLoop As Long 
Dim FoundCell As Range 

    If Column_Lookin = 0 Then 'No column # specified 
     With Table_Range 
      'Top left cell has Find_Val & Occurrence is 1 
      If Table_Range.Cells(1, 1) = Find_Val And Occurrence = 1 Then 
       OzgridLookup = .Cells(1, 1)(1, Offset_Cols + 1) 
       Exit Function 'All done :) 
      Else 'No column # specified so search all for _ 
        nth Occurrence reading left to right 
      Set FoundCell = .Cells(1, 1) 'Set cell variable for Find start 
       For lLoop = 1 To Occurrence 'Loop as many times as Occurrences _ 
       and each time Set "FoundCell" to start next Find from 
        Set FoundCell = _ 
         Table_Range.Find(What:=Find_Val, After:=FoundCell, _ 
          LookIn:=xlValues, LookAt:=xlWhole, _ 
          SearchOrder:=xlRows, SearchDirection:=xlNext) 
       Next lLoop 
      End If 
     End With 
    Else 'column # specified 
     With Table_Range.Columns(Column_Lookin) 'Work with column # specified 
     Set FoundCell = .Cells(1, 1) 'Set cell variable for Find start 
      For lLoop = 1 To Occurrence 'Loop as many times as Occurrences _ 
       and each time Set "FoundCell" to start next Find from 
        Set FoundCell = _ 
         Table_Range.Find(What:=Find_Val, After:=FoundCell, _ 
          LookIn:=xlValues, LookAt:=xlWhole, _ 
          SearchOrder:=xlRows, SearchDirection:=xlNext) 
      Next lLoop 
     End With 
    End If 

    OzgridLookup = FoundCell.Offset(Row_Offset, Offset_Cols) 

End Function 
+0

應該指出的是,匹配CAN可以處理未排序的數組。但是,Match不適用於多維數組,即不能將多列用作搜索範圍(可以返回哪個索引?)。這是錯誤的根源。 – Excellll

+0

@Excellll:我編輯了我的問題來反映這一點 – Zach

回答

5

由於您只想知道號碼是否存在(不是它的位置),您可以使用COUNTIF來做到這一點。

=COUNTIF(A:C,E1)>0 

如果存在則返回「TRUE」,否則返回「FALSE」。

只是爲了好玩,下面是一個工作表函數解決方案,它返回與查找值匹配的單元格地址。它使用的事實是,你只在3列中搜索。

=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)) 

我想我還扔在一個VBA解決方案,它可以返回(連續)範圍內的匹配位置。它從左到右逐列查看一列,並返回找到的第一個匹配的地址。

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