2017-04-26 127 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”。