LibreOffice Calc:我可以从 VLOOKUP 获取单元格地址吗?

LibreOffice Calc: Can I get the cell address from VLOOKUP?

我在 Calc 中使用 VLOOKUP,如下所示:

VLOOKUP(B11,G2:J7,4,0)

通常当我们任何人使用它时,我们都希望获得该函数找到的单元格中的值。在这种情况下,我想获取一个包含单元格地址或该单元格的行和列的字符串,而不是值。例如,如果我在单元格 J5 中有一个双精度浮点值 30.14,这就是答案,而不是 return 30.14,我希望它 return 类似于“J5”或 9, 4 或其他方式让我在宏中读取结果。

我试过使用 =ADDRESS() 和 =CELL("address", ) 但我遇到了错误(=CELL() 给我 '#REF!')。


编辑:我将此例程用作 VLOOKUP 的包装器,带有 table 浮点数(这就是为什么它 return 是 DOUBLE 而不是将单元格值作为 STRING或者是其他东西)。我所要做的就是将它传递给我想要从中获取数据的列:

Function getLookup(valColumn as Integer) as Double
    oDoc = ThisComponent
    oSheet = oDoc.Sheets (workSheet)
    rangeInfo = lookupTopLeft + ":" + lookupBottomRight
    cellRange = oSheet.getCellRangeByName(rangeInfo)
    oCell = oSheet.GetCellByPosition(dataCellColumn, dataCellRow)
    searchValue = oCell.getString()
    Mode = 0
    svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
    args = Array(searchValue, cellRange, valColumn, Mode)
    getLookup = svc.callFunction("VLOOKUP", args)
End Function

请注意,我在此使用了一些局部变量。它们是私有的,仅适用于模块,因此我在设计电子表格时不必在多个地方更改单元格引用。 “lookupTopLeft”和“lookupBottomRight”是“G2”和“J7”,即我正在处理的数据的左上角和右下角单元格。 “dataCellColumn”和“dataCellRow”是我在 VLOOKUP 中使用的键源的列和行坐标。

(@JohnSUN,我认为这可能会根据您在某处提供的答案进行修改。)

我希望能够执行类似的包装例程,该例程将 return 单元格的列和行而不是单元格中的值。

众多可能选项之一:

Option Explicit 

Const lookupTopLeft = "G2" 
Const lookupBottomRight = "J7"
Const dataCellColumn = 1
Const dataCellRow = 10
Const workSheet = 0

Function getCellByLookup(valColumn As Integer) As Variant
Dim oSheet As Variant, cellRange As Variant, oCell As Variant
Dim oColumnToSearch As Variant
Dim oSearchDescriptor As Variant
Dim searchValue As String 
Dim nRow As Long
    oSheet = ThisComponent.getSheets().getByIndex(workSheet)
    cellRange = oSheet.getCellRangeByName(lookupTopLeft + ":" + lookupBottomRight)
    searchValue = oSheet.GetCellByPosition(dataCellColumn, dataCellRow).getString()
    
Rem If we are looking not for a value, but for a cell, 
Rem then using VLOOKUP is unnecessary, a simple Find is enough  
    oColumnToSearch = cellRange.getCellRangeByPosition(0, 0, 0, _
        cellRange.getRows().getCount()-1) ' Resize full range to one first column
Rem Set search params
    oSearchDescriptor = oColumnToSearch.createSearchDescriptor()
    oSearchDescriptor.setSearchString(searchValue)
    oSearchDescriptor.SearchType = 1 ' Search in Values!
Rem Try to find searchValue in oColumnToSearch
    oCell = oColumnToSearch.findFirst(oSearchDescriptor)
    If Not IsNull(oCell) Then ' Only if the value was found
        nRow = oCell.getRangeAddress().StartRow
Rem Offset oCell to valColumn
        oCell = cellRange.getColumns().getByIndex(valColumn-1).GetCellByPosition(0,nRow)
        getCellByLookup = Replace(oCell.AbsoluteName, "$", "")
    Else    ' If the value from B11 is not found - warn about it
        getCellByLookup = "Not found"
    EndIf
End Function