2017-04-26 129 views
-3

我需要在表格及其列中搜索(搜索列A中的值並從列B返回值),並且我不喜歡使用索引/匹配組合因爲我正在爲新手用戶準備工作簿,並且他無法掌握索引/匹配邏輯。索引/匹配VBA替代在表格及其列中搜索

+2

爲什麼不使用VLOOKUP?這比索引/匹配更簡單 –

+0

另外,如果用戶不能學習'Index/Match'(或'VLOOKUP()'),我不確定UDF /宏是否是一個好主意。當然,這可能是「更簡單」,但是隨後他們必須始終啓用宏,這對新手來說可能不是一個好習慣。另外,由於多種原因,學習'Index/Match' /'VLOOKUP()'是非常有用的,所以可能考慮使用它,並且只是教育用戶? – BruceWayne

+1

給用戶一個UDF,而不是教他們INDEX/MATCH。嗯......聽起來像是給一個不能點燃比賽的人手槍。 – Jeeped

回答

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」。