我在工作表中有两列,如下面的列表。
我想做的是从第二列中提取值,但前提是相应的第一列值与我指定的值匹配。我可以使用 Vlookup 来做到这一点,但是我还希望能够从数组中指定第一列匹配的行号。
例如,如果我将值设置为“2”并将行号设置为“2”,它将给我值 14,因为它与第 1 列中的第一个值匹配并创建了一个数组,然后给了我该数组的第二行是 14。
有任何想法吗?
1 10
1 11
1 12
2 13
2 14
2 15
3 16
3 17
3 18
如果您的数据按照示例中的方式排序,即。通过第一列升序,然后第二列升序。然后您可以使用OFFSET(),INDEX()和的组合MATCH()。
=OFFSET(INDEX(second_column,MATCH(lookup_a,first_column,0)),entry_row-1,0)
这首先获取与INDEX(..., MATCH())您的查找值匹配的第一个单元格的单元格地址,然后该OFFSET()函数通过您想要在该组中的行数将其击倒。当然,这完全取决于所描述的排序。

如果您想要未排序的第二列中的第 k 个最小条目(但仍分组在第一列),那么您可以使用:
=SMALL(OFFSET(second_column,MATCH(lookup_a,first_column,0)-1,0,MATCH(lookup_a,first_column,1)-MATCH(lookup_a,first_column,0)+1,1),entry_row)
#N/A如果您超出选择范围,这具有返回的优势。用IFERROR(...., "").
或者,对未排序的第二列数据使用第一种方法并仅检查偏移量以查看它是否保留查找值,您可以使用:
=IF(OFFSET(OFFSET(INDEX(second_column,MATCH(lookup_a,first_column,0)),entry_row-1,0),0,-1)=lookup_a,OFFSET(INDEX(second_column,MATCH(lookup_a,first_column,0)),entry_row-1,0),"")
回答使用 VBA 选择“查找”的第二个结果可以扩展到您的案例,使用您要查找的出现次数作为输入。
这是对Excel Lookup nth Occurrence/Instance的改编:
Function FindNth(Table As Range, Val1 As Variant, Val1Occrnce As Integer, ResultCol As Integer)
'Finds the Nth value in the first Column of a table that has a stated value on the same row in another Column.
Dim i As Integer
Dim iCount As Integer
Dim rCol As Range
iCount = 0
For i = 1 To Table.Rows.Count
If (Table.Cells(i, 1) = Val1) Then
iCount = iCount + 1
End If
If iCount = Val1Occrnce Then
FindNth = Table.Cells(i, ResultCol)
Exit For
End If
Next i
End Function
这是使用用户定义函数的结果(公式栏显示单元格 G3 中的公式):
