我需要在表格及其列中搜索(搜索列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