[英]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:我希望文件像这样工作:
Things I've tried:我尝试过的事情:
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.