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
应该指出的是,匹配CAN可以处理未排序的数组。但是,Match不适用于多维数组,即不能将多列用作搜索范围(可以返回哪个索引?)。这是错误的根源。 – Excellll
@Excellll:我编辑了我的问题来反映这一点 – Zach