【问题标题】:Lookup value from a cell which has multiple values excel从具有多个值的单元格中查找值 excel
【发布时间】:2019-11-18 01:05:21
【问题描述】:

我下载了一个项目计费报告,其中 A 列中的名字和 B 列中的费用代码。一个单元格中有多个费用代码,用于确定它是计费还是不可计费或错误的费用代码输入。

我在使用以下索引匹配公式时遇到了困难。有没有办法从具有多个用逗号分隔的值的单元格中查找值?

=INDEX(H2:H4,MATCH(H2,B2:B4,0))

Excel 计费示例:

【问题讨论】:

    标签: excel vba indexing match


    【解决方案1】:

    如果您的代码列表的格式始终相同,即前 4 个字符是代码,后跟逗号和空格,然后是下一个代码,您可以使用“if”和“mid”语句来获取每个代码

    =VLOOKUP(MID(B2,1,4),$H$2:$I$4,2,FALSE) & IF(LEN(B2)>5,", " & VLOOKUP(MID(B2,7,4),$H$2:$I$4,2,FALSE),"")
    

    对不起,我倾向于使用 vlookup 而不是索引和匹配。该公式适用于两个代码,根据需要重复公式的第二部分以获得尽可能多的代码。

    祝你好运!

    【讨论】:

      【解决方案2】:

      看到工作表布局后更新答案...可能是这样的(匹配错误已更正)...

      Option Explicit
      
      Public Function LookupCode(ChargeCode As Range, vChargeCodes As Range) As Boolean
      
          Dim vRow As Long
          Dim vLookupCode As String
      
          LookupCode = False
          vRow = 2
          Do While LookupCode = False And ActiveSheet.Cells(vRow, 9) <> ""
              If ActiveSheet.Cells(vRow, 9) = ChargeCode Then
                  If IsInArray(ActiveSheet.Cells(vRow, 8), Split(vChargeCodes, ",")) Then
                      LookupCode = True
                      Exit Do
                  End If
              End If
              vRow = vRow + 1
          Loop
      
      End Function
      
      ' variation of IsInArray from https://stackoverflow.com/questions/38267950/check-if-a-value-is-in-an-array-or-not-with-excel-vba
      Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
          Dim vIndex As Long
          For vIndex = LBound(arr) To UBound(arr)
              If Trim(arr(vIndex)) = Trim(stringToBeFound) Then
                  IsInArray = True
                  Exit Function
              End If
          Next
          IsInArray = False
      End Function
      

      然后在工作表中...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-05
        • 2023-03-16
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多