简体   繁体   English

Excel:使用vlookup但在数组中使用通配符

[英]Excel: using vlookup but with wildcards in the array

I have a list of values in a column on one sheet. 我在一张纸上的列中有一个值列表。 On another sheet I have two columns. 在另一张纸上,我有两列。 One is a list of wildcards, and the other is another list of values. 一个是通配符列表,另一个是另一个值列表。 Next to the column on the first sheet, I want an additional column to contain a formula that will check the value in the first column against the wildcards in the second sheet. 在第一张工作表的列旁边,我想要一个额外的列来包含一个公式,该公式将根据第二个工作表中的通配符检查第一列中的值。 If a match is found, it should display the value next to that wildcard. 如果找到匹配项,则应显示该通配符旁边的值。

Is there any way to do this? 有没有办法做到这一点? Been at it for hours and I can't get it to work. 已经好几个小时,我无法让它工作。

Thanks in advance. 提前致谢。

Some sample data: 一些样本数据:

Sheet One 第一张

Column A A栏

randomunnecessarydataUSEFULTHINGS123INFOmoregarbage randomunnecessarydataUSEFULTHINGS123INFOmoregarbage

morerandomstuffIMPORTANT456junkjunkjunk morerandomstuffIMPORTANT456junkjunkjunk

IMPORTANT456lotsofmorejunk IMPORTANT456lotsofmorejunk

morejunkUSEFULTHINGS789INFOgarbage morejunkUSEFULTHINGS789INFOgarbage

Column B B栏

<some formula>

<some formula>

"

"

etc. 等等

Sheet Two 第二张

Column A A栏

*usefulthings???INFO*

*important456*

Column B B栏

Useful Things - Info 有用的东西 - 信息

Important 456 重要456

I want <some formula> to check the value in sheet 1 column A next to it against the table in Sheet 2. If one of the wildcard in sheet 2 column A match, the cell containing the formula should display what's in Sheet 2 column B. 我想要<some formula>来检查它旁边的工作表1列A中的值与工作表2中的表。如果工作表2列A中的一个通配符匹配,则包含公式的单元格应显示工作表2列B中的内容。

If I got you right you want something like this (in B1 and then copy down): 如果我说得对,你想要这样的东西(在B1然后复制下来):

=IF(MIN(IFERROR(MATCH(Sheet2!$A$1:$A$100,A1,0)*ROW($1:$100),FALSE)),INDEX(Sheet2!B:B,MIN(IFERROR(MATCH(Sheet2!$A$1:$A$100,A1,0)*ROW($1:$100),FALSE))),"")

This is an array formula and must be confirmed with ctrl + shift + enter . 这是一个数组公式,必须使用ctrl + shift + enter确认

在此输入图像描述
Left is sheet 1 with the lookup term and right is sheet2 with the wildcard-list and the values. 左边是带有查找词的第1页,右边是带有通配符列表和值的sheet2。

EDIT 编辑
To auto-range it simply use this formula: 要自动调整范围,只需使用以下公式:

=IF(MIN(IFERROR(MATCH(Sheet2!$A$1:INDEX(Sheet2!A:A,MATCH("zzz",Sheet2!A:A)),A1,0)*ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A))),FALSE)),INDEX(Sheet2!B:B,MIN(IFERROR(MATCH(Sheet2!$A$1:INDEX(Sheet2!A:A,MATCH("zzz",Sheet2!A:A)),A1,0)*ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A))),FALSE))),"")

And yes, only the first value will be outputted... if you want to get multiple values, you could use this formula (as always in B1 and then copy down and this time also to the left): 是的,只输出第一个值...如果你想获得多个值,你可以使用这个公式(如在B1中一样,然后向下复制,这次也在左边):

*² =IFERROR(IF(SMALL(IFERROR(MATCH(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A)),$A1,0)*ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A))),FALSE),COLUMN()-1),INDEX(Sheet2!$B:$B,SMALL(IFERROR(MATCH(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A)),$A1,0)*ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A))),FALSE),COLUMN()-1)),""),"")

But keep in mind that all the formulas will slow down your excel the more cells use them (the last one the most). 但请记住,所有公式都会减慢你的excel,更多的细胞使用它们(最后一个)。 If your list is pretty long, I suggest a UDF. 如果你的列表很长,我建议使用UDF。

EDIT 2 编辑2

To speed it a bit up again you can use this formula for C1 (copy down / right) (just use the last formula only for column B): 为了再次加快速度,您可以将此公式用于C1(向下/向右复制)(仅对B列使用最后一个公式):

=IF(B1="","",IFERROR(IF(SMALL(IFERROR(MATCH(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A)),$A1,0)*ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A))),FALSE),COLUMN()-1),INDEX(Sheet2!$B:$B,SMALL(IFERROR(MATCH(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A)),$A1,0)*ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A,MATCH("zzz",Sheet2!$A:$A))),FALSE),COLUMN()-1)),""),""))

Also keep in mind that all formulas are array-formulas ;) 还要记住,所有公式都是数组公式;)

*² If using the first EDIT formula for column B and the EDIT 2 formula for column C+ you do not need the second EDIT formula. *²如果使用B列的第一个EDIT公式和C +列的EDIT 2公式,则不需要第二个EDIT公式。

EDIT 3 编辑3

For the UDF-way go to your VBA-editor (hit alt + F11 ) and there "Insert" -> "Module". 对于UDF方式,转到VBA编辑器(按下alt + F11 ),然后“插入” - >“模块”。 Then put in the code window for this module: 然后放入此模块的代码窗口:

Option Explicit

Public Function getLikeLookup(str As String, rng As Range, Optional nCou As Long, Optional outCol As Range) As String

  'for not case sensitive
  str = LCase(str)

  'set ranges
  If nCou < 1 Then nCou = 1
  If outCol Is Nothing Then Set outCol = rng.Offset(, rng.Columns.Count - 1).Resize(, 1)
  Set rng = Intersect(rng.Resize(, 1), rng.Parent.UsedRange.EntireRow)
  Set outCol = Intersect(outCol.Resize(, 1), outCol.Parent.UsedRange.EntireRow)

  'get check-array (will be faster than running the sheet directly)
  Dim inArr As Variant
  inArr = rng.Value

  'run checks
  Dim i As Long
  For i = 1 To UBound(inArr)
    'If str Like inArr(i, 1) Then nCou = nCou - 1
    If str Like LCase(inArr(i, 1)) Then nCou = nCou - 1 'for not case sensitive
    If nCou = 0 Then Exit For
  Next

  'check for valid output
  If i > UBound(inArr) Or i > outCol.Rows.Count Then Exit Function

  'set output
  getLikeLookup = outCol.Offset(i - 1).Resize(1, 1).Value

End Function

Now you are able to use your UDF like other worksheet functions. 现在,您可以像使用其他工作表函数一样使用UDF。 To explain this in detail. 详细解释一下。

getLikeLookup(lookup_string,lookup_range,[#_occurrence,[output_range]])
  • lookup_string : the string you want to check against (the whole string, does not work with placeholder) lookup_string :要检查的字符串(整个字符串,不适用于占位符)

  • lookup_range : the leftmost column in this range will be checked to be like lookup_string . lookup_range :在此范围内最左列将进行检查,以 lookup_string If output_range is omitted then the rightmost column in lookup_range will be used for the output. 如果output_range省略然后在最右边的列lookup_range将用于输出。

  • #_occurrence : [optional] Indicates which match to output. #_occurrence :[可选]指示要输出的匹配项。 If omitted (like 1) than the first one will be picked. 如果省略(如1),则将挑选第一个。

  • output_range : [optional] the first column in output_range will be used for the output. output_range :[可选]在第一列output_range将用于输出。

Now for the example you can use (starting in B1): 现在您可以使用(从B1开始)的示例:

=getLikeLookup($A1,Sheet2!$A:$B,COLUMN()-1)

And to speed it up a bit use(still starting in B1): 并加快一点使用(仍然从B1开始):

=IF(A1="","",getLikeLookup($A1,Sheet2!$A:$B,COLUMN()-1))

This 2 formulas are no arrays and can be confirmed by just hitting enter . 这2个公式没有数组,可以通过点击输入来确认。

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

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