我需要在表格及其列中搜索(搜索列A中的值并从列B返回值),并且我不喜欢使用索引/匹配组合因为我正在为新手用户准备工作簿,并且他无法掌握索引/匹配逻辑。索引/匹配VBA替代在表格及其列中搜索
-3
A
回答
0
我没有找到任何简单/优雅的公式来做到这一点(除了索引/匹配组合),然后我写了下面的VBA代码。
作为奖励,如果有多个相应的行匹配,函数会自动从列中求和值以读取值。
' return a value taken from a column searching for a value in another column;
' the function works in the same way as Index(Match());
' if the column ColNameToRead contains number values, and if ValueToSearch is found more then one time in the column ColNameToSearch, then the returned value
' is the sum of all occurrence found.
Public Function SearchValInCol(TabName As String, ColNameToSearch As String, ValueToSearch As Variant, ColNameToRead As String)
Application.Volatile (True) ' see "Excel Recalculation" https://msdn.microsoft.com/en-us/library/office/bb687891.aspx
SearchValInCol = "Error, value not found!"
' search table in all Worksheets; exit if not found
Dim foundTable
Dim objSheet
Dim objTable
foundTable = 0
For Each objSheet In ActiveWorkbook.Sheets
For Each objTable In objSheet.ListObjects
If objTable.Name = Trim(TabName) Then
Set foundTable = objTable
Exit For
End If
Next
Next
If IsNumeric(foundTable) Then Exit Function ' exit function if the table is not found
' search column named ColNameToSearch in table; exit if not found
Dim foundColumnToSearch
Dim counter
foundColumnToSearch = 0
For counter = 1 To foundTable.ListColumns.Count
If foundTable.HeaderRowRange(counter) = Trim(ColNameToSearch) Then
Set foundColumnToSearch = foundTable.ListColumns(counter).DataBodyRange
Exit For
End If
Next counter
If IsNumeric(foundColumnToSearch) Then Exit Function ' exit function if the column is not found
' search column named ColNameToRead in table; exit if not found
Dim foundColumnToRead
foundColumnToRead = 0
For counter = 1 To foundTable.ListColumns.Count
If foundTable.HeaderRowRange(counter) = Trim(ColNameToRead) Then
Set foundColumnToRead = foundTable.ListColumns(counter).DataBodyRange
Exit For
End If
Next counter
If IsNumeric(foundColumnToRead) Then Exit Function ' exit function if the column is not found
' search value ValueToSearch in column foundColumnToSearch; exit if not found
Dim cellVal
Dim retVal
retVal = 0
For counter = 1 To foundColumnToSearch.Rows.Count
If foundColumnToSearch.Cells(counter, 1) = ValueToSearch Then
' if the value to search is a number, sum it with the previous value; otherwise return the first occurrence
If IsNumeric(foundColumnToRead.Cells(counter, 1)) Then
retVal = retVal + foundColumnToRead.Cells(counter, 1)
Else
retVal = foundColumnToRead.Cells(counter, 1)
Exit For
End If
End If
Next counter
SearchValInCol = retVal
End Function
为下表(表1)中的“文章”“维诺量”从柱返回一个值“”
article quantity
vino 7
acqua 8
patate 5
vino 7
在内部搜索可以使用下面的公式:
=SearchVal("Table1"; Table1[[#Headers];[article]]; "vino"; Table1[[#Headers];[quantity]])
0
另外,简单的解决方案,始终与VBA,如下
' return a value taken from a column searching for a value in another column;
' the function works in the same way as Index(Match());
' if the column ColToRead contains number values, and if ValueToSearch is found more then one time in the column ColToSearch, then the returned value
' is the sum of all occurrence found.
Public Function SearchValInCol2(ColToSearch, ValueToSearch As Variant, ColToRead)
Application.Volatile (True) ' see "Excel Recalculation" https://msdn.microsoft.com/en-us/library/office/bb687891.aspx
SearchValInCol2 = "Error, value not found!"
' search value ValueToSearch in column ColToSearch; exit if not found
Dim counter
Dim cellVal
Dim retVal
retVal = 0
For counter = 1 To ColToSearch.Rows.Count
If ColToSearch.Cells(counter, 1) = ValueToSearch Then
' if the value to search is a number, sum it with the previous value; otherwise return the first occurrence
If IsNumeric(ColToRead.Cells(counter, 1)) Then
retVal = retVal + ColToRead.Cells(counter, 1)
Else
retVal = ColToRead.Cells(counter, 1)
Exit For
End If
End If
Next counter
SearchValInCol2 = retVal
End Function
要在下表中搜索“表1”
article quantity
vino 7
acqua 8
patate 5
vino 7
公式调用该函数可以是:
=SearchValInCol2(Table1[article];"vino";Table1[quantity])
是更简明易读先前的溶液,并避免使用内部的公式化表格名称将更改时不会更新的文字字符串“Table1”。
相关问题
- 1. VBA索引匹配
- 2. 搜索匹配的VBA
- 3. VBA索引匹配错误
- 4. Excel 2010索引匹配VBA
- 5. Excel VBA索引和匹配
- 6. 索引与匹配代替VLOOKUP
- 7. 正在搜索匹配的主列表
- 8. Word VBA通配符搜索匹配
- 9. VBA中的索引匹配2条件
- 10. 找不到FULLTEXT索引匹配列的列表(索引设置)
- 11. Python - 列表搜索重复不匹配?
- 12. 搜索链接列表匹配
- 13. 多条件匹配/搜索vba
- 14. 搜索和匹配表
- 15. Typo3搜索索引引擎Html表格
- 16. 在lucene索引搜索中缺少匹配
- 17. Excel VBA索引匹配动态范围
- 18. 搜索并替换多个匹配项
- 19. 找到正则表达式搜索/匹配的结束索引
- 20. Excel VBA:在几个阵列之间复制索引(匹配())
- 21. 替代具有索引和匹配VLOOKUP在Excel
- 22. Mysql搜索引擎喜欢和匹配
- 23. Sharepoint搜索引擎模糊匹配?
- 24. 搜索和阵列中的替代
- 25. 搜索引擎及的iFrame
- 26. 匹配索引值
- 27. 节点弹性搜索严格匹配
- 28. 在Python中搜索列表以查找匹配项?
- 29. Erlang:在列表中搜索匹配的字符串
- 30. 索引匹配,其中匹配号码不准确
为什么不使用VLOOKUP?这比索引/匹配更简单 –
另外,如果用户不能学习'Index/Match'(或'VLOOKUP()'),我不确定UDF /宏是否是一个好主意。当然,这可能是“更简单”,但是随后他们必须始终启用宏,这对新手来说可能不是一个好习惯。另外,由于多种原因,学习'Index/Match' /'VLOOKUP()'是非常有用的,所以可能考虑使用它,并且只是教育用户? – BruceWayne
给用户一个UDF,而不是教他们INDEX/MATCH。嗯......听起来像是给一个不能点燃比赛的人手枪。 – Jeeped