【问题标题】:Excel - search part of a string, vlookup value in tableExcel - 搜索字符串的一部分,表格中的vlookup值
【发布时间】:2018-07-17 14:19:01
【问题描述】:

我在 Excel 中有两个表格:

table 1 中,一列包含数据字符串。格式不是结构化的,但总是包含我试图与table 2 中的列匹配的关键字。示例:

32984723987 BLA BLA &*& KEYWORD 232328 BLA

table 2 中,第一列包含关键字。第二列子类别标签。

如果table 1 中存在关键字,我如何使用vlookup 或其他公式填写table 2 中的匹配子类别?

我一直在尝试范围查找,但我需要输入一些搜索条件。但是哪一个?我需要检查table 2 中的整个关键字列。

编辑:为了更清楚:lookup_value 需要检查单元格 Sheet1!A1 的部分字符串(关键字)是否存在于 Sheet2!A1:A100 中的任何位置(整个 列的关键字)。如果存在,公式应返回 Sheet2!Bx 的值(找到匹配关键字的行号)。

任何帮助将不胜感激。

【问题讨论】:

    标签: excel string range match vlookup


    【解决方案1】:

    您只需要在 vlookup 中使用通配符,例如:

    =VLOOKUP("*"&table2!A1&"*", table1!A:A, 1, FALSE)
    

    编辑:使用此 VBA 代码(已编辑代码)进行操作

    Sub findKeywords()
    
    Dim wb As Workbook
    Dim rWs, kWs As Worksheet
    Dim dataColumn, targetColumn, keysColumn As Integer
    
    Set wb = ThisWorkbook
    Set rWs = wb.Sheets("rawSheet") 'this is the sheet your raw data is
    Set kWs = wb.Sheets("keySheet") 'this is your sheet with the keys
    dataColumn = 1 'this is the number of the column you will search
    targetColumn = 2 'this is the number of the column you will insert the matched row
    keysColumn = 1 'this is your column with the keywords you will look for
    
    For Each r In rWs.Range(rWs.Cells(1, dataColumn).Address, rWs.Cells(rWs.Rows.Count, dataColumn).End(xlUp).Address)
        For i = 1 To kWs.Cells(kWs.Rows.Count, keysColumn).End(xlUp).Row
            If r.Value Like "*" & kWs.Cells(i, keysColumn).Value & "*" Then
                r.Offset(0, 1).Value = kWs.Cells(i, keysColumn).Offset(0, 1).Value
                Exit For
            End If
        Next i
    Next r
    
    End Sub
    

    【讨论】:

    • 非常感谢,但这并不能解决问题。 lookup_value 需要检查Sheet1!A1 中的部分字符串(关键字)是否存在于Sheet2!A1:A100(关键字的整个 列)的任何位置。如果存在,它应该返回 Sheet2!Bx 的值(找到匹配关键字的行号)。
    • 在这种情况下,您将需要一个 vba 解决方案,稍等我可以想出一个
    • 非常感谢。它填充了它应该填充的单元格,但没有填充预期值......它不是返回 sheet2 的 B 列中的标签,而是返回数字,并且数字与匹配关键字的行不对应。好像有很多61、69、110,不知道为什么。我觉得代码很接近,但还没有。我不是程序员,所以无法修复它。任何建议都会非常受欢迎。非常感谢您迄今为止的帮助!
    • 好的,我会再试一次,有没有办法让您将部分内容放在谷歌表格上并分享,这样我就可以复制到我的 excel 并根据您的需要调整代码?
    • 好的,代码首先选择了一个数字,因为我认为您想要 sheet2 中的关键字行,我只是调整了这部分以获得关键字表中 B 列的值。至于不正确的行,如果你的意思是所有行我不明白为什么,因为它在这里工作,但如果它只是用于某些行中的关键字,那可能是因为它们是小词并且某些地址与它们匹配跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2011-08-10
    • 2018-04-12
    相关资源
    最近更新 更多