简体   繁体   English

如果在其中一列中找到查找值,则返回第一列的值

[英]Return value of the first column if lookup value is found in one of columns

I'm trying to create simple Excel file that will help my team compare electronic component names in machine programs with those which are specified in work instructions.我正在尝试创建简单的 Excel 文件,以帮助我的团队将机器程序中的电子元件名称与工作说明中指定的名称进行比较。 The problem is, when the machine returns file with list of components, it looks like this (I want this to be the first / main sheet):问题是,当机器返回带有组件列表的文件时,它看起来像这样(我希望这是第一个/主表):

FIRST SHEET

Variation_Name   Location   Component_No. 
0160_7988_0001   C353       0160_7988_0001
0160_7988_0001   C348       0160_7988_0001
0160_8881_0001   C368       0160_8881_0001
0160_8881_0001   C311       0160_8881_0001
0160_8881_0001   C439       0160_8881_0001
0160_8881_0001   C429       0160_8881_0001
0160_8881_0001   C441       0160_8881_0001
0160_8881_0001   C442       0160_8881_0001
0160_8881_0001   C428       0160_8881_0001

So as you can see, each component location is listed in separate row with Variation Name / Component No. being repeated.因此,如您所见,每个组件位置都列在单独的行中,并重复了变体名称/组件编号。 But the component lists from work instructions look like this (I want this to be the second sheet from which I will extract data):但是工作说明中的组件列表看起来像这样(我希望这是我将从中提取数据的第二张表):

SECOND SHEET

Material             Locations
0160-7751-0001       C119
0160-7988-0001       C348, C353
0160-7988-0001       C347, C350, C351
0160-8881-0001       C311, C315, C316, C352, C355, C368
0160-8881-0001       C126, C313, C317, C346, C349, C354, C402, C407
0160-9135-0001       C213
0160-9158-0001       C114, C438, C439, C441, C442
0160-9210-0001       C343
0160-9213-0001       C101, C104, C109, C203, C207, C211, C215, C218, C219

Each material has multiple locations listed but NOT in separate rows which for me, the guy who always worked with VLOOKUPs on data that was wonderfully formatted is just an overkill...每个材料都列出了多个位置,但不是在单独的行中,这对我来说,总是使用 VLOOKUP 处理格式非常好的数据的人只是一种矫枉过正......

I wanted the file to work like this:我希望文件像这样工作:

  1. In fourth row of the first sheet (fe Instr_Compo) look up for value from "Location" row, somewhere in the second sheet在第一张纸的第四行(fe Instr_Compo)从“Location”行中查找值,位于第二张纸的某处
  2. If value is found in the second sheet, return the "Material" value如果在第二张表中找到值,则返回“材料”值
  3. Later compare returned value with value from "Component No."稍后将返回值与“组件编号”中的值进行比较。 (I know how to do this obviously) (我显然知道如何做到这一点)

Things I've tried:我尝试过的事情:

  1. Because of "Locations" being listed in second sheet in the same cell and separated by space and comma, I used "Text to columns" to move each value to separate column由于“位置”列在同一单元格的第二个工作表中并用空格和逗号分隔,因此我使用“文本到列”将每个值移动到单独的列
  2. Then I've tried multiple combinations with HLOOKUP, VLOOKUP, INDEX and MATCH, changing the layout, etc. etc... with no result except for frustration.然后我尝试了 HLOOKUP、VLOOKUP、INDEX 和 MATCH 的多种组合,更改布局等......除了沮丧之外没有任何结果。
  3. Tried looking up for the solution on the net but I don't want to use visual basic as I don't know how to write in it and I don't have time to experiment with it.试图在网上寻找解决方案,但我不想使用visual basic,因为我不知道如何写,而且我没有时间去试验它。

Probably there is something really simple I haven't tried but I'm out of ideas.可能有一些非常简单的东西我没有尝试过,但我没有想法。

You could achieve this using VBA like below:您可以使用 VBA 实现这一点,如下所示:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
Dim wsData As Worksheet: Set wsData = Sheets("Sheet2")
'declare and set the worksheets you are working with, amend as required

LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
'get the last row with data on Column B

For i = 4 To LastRow 'loop from row 4 to last
    LookUpValue = ws.Cells(i, "B").Value 'get the value you are searching for
    Set FoundVal = wsData.Range("B:B").Find(What:=LookUpValue, LookAt:=xlPart)
    'use the .Find method to look for the value in Column B on the second sheet
    If Not FoundVal Is Nothing Then 'if found
        ws.Cells(i, "D").Value = FoundVal.Offset(0, -1).Value
        'get the material number into Column D on your first sheet.
    End If
Next i
End Sub

UPDATE:更新:

You could also use a combination of Index Match with wildcards like below:您还可以使用索引匹配与通配符的组合,如下所示:

=INDEX(Sheet2!$A:$B,MATCH("*" & B5 & "*",Sheet2!$B:$B,0),1)

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

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