从多列中查找值的位置

Finding location of value from multiple columns

我有一辆 4x4 table。 table 中的所有值都是唯一的。

    A   B   C   D
1   a   b   c   d
2   e   f   g   h
3   i   j   k   l
4   m   n   o   p

如何找到特定值的 address?我已经尝试 =CELL("address",MATCH(A1,A1:D4,0)) 找到 a 但它 returns 是一个错误值,因为 =MATCH(A1,A1:D4,0) 是一个错误值。

似乎 =match 只适用于单个 row/column。

有什么解决办法吗?

您可以将以下内容与不同的值一起使用

=CELL("ADDRESS",INDEX(A1:D4,SMALL(IF(NOT(ISERROR(SEARCH("a",A1:D4))),ROW(1:4),99^99),1),SMALL(IF(NOT(ISERROR(SEARCH("a",A1:D4))),COLUMN(A:D),99^99),1)))

使用Ctrl + Shift+Enter [=17= 作为数组公式输入]

数据:


您可以将要查找的内容移动到单独的单元格中,而不是将其硬编码到公式中,并将整个内容包装在 IFERROR 中以防找不到,例如

在 G1 中搜索值,在 F1 中搜索公式。

=IFERROR(CELL("ADDRESS",INDEX(A1:D4,SMALL(IF(NOT(ISERROR(SEARCH(G1,A1:D4))),ROW(1:4),99^99),1),SMALL(IF(NOT(ISERROR(SEARCH(G1,A1:D4))),COLUMN(A:D),99^99),1))),"")

这 returns 找到的行:

SMALL(IF(NOT(ISERROR(SEARCH("a",A1:D4))),ROW(1:4),99^99),1)

此 returns 找到的列:

SMALL(IF(NOT(ISERROR(SEARCH("a",A1:D4))),COLUMN(A:D),99^99),1)

然后将这些用于索引范围

的交集
INDEX(A1:D4,.......)

这是另一种方法(没有数组公式,即不按 cntrl-enter),基于 this solution to locate a value in a 2D array.

为了在作者的网站上正确地归功于作者,我不会重复对其工作原理的精彩解释。

EDIT: Based on @Chronocidal's excellent suggestions, here is the new and improved version for reference:

最后一个使用 VBA:

Function FINDaddress(VALUESEARCHED As String, ra As Range) As Variant

Dim A As Range

    Set A = ra.Find(What:=VALUESEARCHED, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

FINDaddress = A.Address

End Function

之后你只需要使用自制函数:

=FINDaddress("a",A1:D4)

您可以通过 VBA 创建自定义 Function:


findIn(what, where)

returns a "A1" styled String of a sought element (what) in a range/array (where).
If not contained inside the searched range, returns "Not found"

Private Function findIn(ByVal what As String, ByVal where As Range) As String

   Dim res As Range
   Set res = where.Find(what, LookIn:=xlValues)

   If Not res Is Nothing Then
     findIn = Col_Letter(res.Column) & res.Row 'returns A1 styled result
   Else
     findIn = "Not Found"
   End If

End Function

'col number to letter by brettdj
'(so.com)/questions/12796973/function-to-convert-column-number-to-letter/49577009)
Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

所以如果我们要测试它,会产生预期的结果:

Private Sub test()
    Range("E1") = findIn("d", Range("A1:D4"))
End Sub

或者,如果将其放在单独的模块中 (Insert -> Module)

,您也可以将其用作工作表函数