简体   繁体   English

VBA:多个关键字vlookup

[英]VBA: Multiple Keyword vlookup

I have a number of narrative descriptions that I need to categorize automatically in Excel: 我有一些叙述性描述,需要在Excel中自动进行分类:

Description                       Category
I updated the o.s.  
I installed the o.s.    
I cleaned valve a   
I cleaned valve b   
I installed valve a 
Today the o.s. was updated

I have another worksheet with keywords and the category the keywords are associated with: 我还有另一个工作表,其中包含关键字以及与关键字相关联的类别:

Keyword 1   Keyword 2   Keyword 3   Category
cleaned      valve         a           A
installed    valve         a           B
updated       os                       C
installed     os                       D

My code so far can only search one keyword at a time and therefore will report incorrect answers because some keywords are used in multiple narratives: 到目前为止,我的代码一次只能搜索一个关键字,因此会报告错误的答案,因为在多个叙述中使用了一些关键字:

Public Function Test21(nar As Range, ky As Range) As String

Dim sTmp As String, vWrd As Variant, vWrds As Variant

'Splits Fsr Narrative into individual words so it can be searched for keywords'
vWrds = Split(nar)

For Each vWrd In vWrds

    If Not IsError(Application.VLookup(vWrd, ky, 3, False)) Then
    sTmp = Application.VLookup(vWrd, ky, 3, False)
    Exit For
    End If
Next vWrd

Test21 = sTmp

End Function    

I've seen algorithms like this but I feel that my goal could be simpler to accomplish as all narratives are relatively simple. 我见过像算法, 可是我觉得我的目标可以简单完成所有的叙述都是比较简单的。

Thanks for reading! 谢谢阅读!

You can match multiple columns with a VLOOKUP by creating a "match column" that concatenates the multiple values together, then searching that column for a match. 通过创建将多个值连接在一起的“匹配列”,然后在该列中搜索匹配项,可以将多个列与VLOOKUP匹配。

So if you use this formula in column A: 因此,如果您在A列中使用此公式:

=B1 & "|" & C1 & "|" & D1

You can then VLOOKUP against that match column: 然后,您可以针对该匹配列进行VLOOKUP:

=VLOOKUP("blah|bleh|ugh", 'Sheet2!A1:E100', 5, FALSE)

Which will match the one row that has "blah" in column B, "bleh" in column C, and "ugh" in column D, and return the value in column E. 它将匹配在B列中具有“ blah”,在C列中具有“ bleh”和在D列中具有“ ugh”的一行,并在E列中返回值。

For your data though, I think you might also want to have a step to clean up your input before trying to match a set of keywords. 不过,对于您的数据,我想您可能还想在尝试匹配一组关键字之前先整理一下输入内容。 The method I described above works best if the keywords are in a particular order, and where you won't have any non-keywords cluttering up things. 如果关键字是按特定顺序排列的,并且没有任何非关键字会造成混乱,那么我上面描述的方法最有效。 (It also works excellently for vlookups where you want to match multiple pieces of data, ie. first name, middle name, and last name in different columns) (它对于要匹配多个数据的vlookups也非常有用,例如,不同列中的名字,中间名和姓氏)

Otherwise you could end up needing an incredibly huge number of rows in your category table to cover every possible combination and permutation of your keywords and the other random words they might be accompanied by. 否则,您可能最终需要在类别表中包含大量行,以涵盖关键字以及它们可能伴随的其他随机单词的所有可能组合和排列。

This is what I was looking for: 这就是我想要的:

Public Function Test22(nar As Range, key As Range, cat As Range) As String

For r = 1 To key.Height
If InStr(nar, key(r, 1)) And InStr(nar, key(r, 2)) Then
Test22 = cat(r)
Exit For
End If
Next r

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM