首页 > 解决方案 > 在 VBA 中使用索引匹配函数从不同工作表上的表中查找附加数据

问题描述

我在 A1:O27 的每张表上有 6 个表...我正在尝试使用索引和匹配从这些表中获取数据;名称 (A1:O1) 和用户输入值 (A2:O27)。我已经让它在一个普通的 excel 电子表格上工作,但它非常草率。我想制作一个整体功能来为我进行搜索。目标是先搜索名称,获取列值,然后找到用户输入值对应的行。然后,使用已知行,我需要索引到倒数第二列,以获得相应的值作为函数的输出。如果每个表格的尺寸和布局都相同,我该如何根据用户输入更改工作表。是否需要将其放入循环中以搜索正确的位置,或者我可以只使用索引/匹配功能吗?这是我到目前为止所拥有的:

Function findSPL(Sonar As String, Distance As String, Condition As String) As String

Dim sonarcol As Range
Dim Dis As Range
Dim c2 As Integer
Dim r2 As Integer

'Set sonarcol to be equal to the "headers"'
'Set Dis to be equal to the table's data'
'Condition is equal to the selected table from drop down list user selects'

Set sonarcol = Worksheets(Condition).Range("A1:O1")
Set Dis = Worksheets(Condition).Range("A2:O27")

'First find the Column value that has the Sonar's name (Exact)'
'Using the column value in c2, find the row that has the user input distance'

c2 = Application.WorksheetFunction.Match(Sonar, sonarcol, 0)
r2 = Application.WorksheetFunction.Index(Dis, WorksheetFunction.Match(Distance, Dis, 1), c2)



'Use the row value from r2 to find the SPL value from column 14 row r2'
findSPL = Application.WorksheetFunction.Index(Dis, r2, 14)


End Function

该图像显示了使用的表格之一以及我正在尝试做的事情。(找到列号,然后找到相应的行号...从该行号中找到 N (14) = 185 列中的 SPL 值

标签: excelvbaexcel-formula

解决方案


推荐阅读