我正在尝试创建简单的Excel文件,这将帮助我的团队将机器程序中的电子组件名称与工作说明中指定的名称进行比较。问题是,当机器返回带有组件列表的文件时,它看起来像这样(我希望这是第一张/主页):
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
因此,如您所见,每个组件位置在单独的行中列出,并且重复名称/组件编号。但是工作说明中的组件列表如下所示(我希望这是提取数据的第二张纸):
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
每种材料都列出了多个位置,但没有在单独的行中列出,这对我来说,一直使用VLOOKUP处理格式精美的数据的人实在是太过分了...
我希望文件像这样工作:
我尝试过的事情:
可能有些简单的事情我没有尝试过,但是我没有想法。
答案 0 :(得分:1)
您可以使用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
更新:
您还可以结合使用索引匹配和通配符,如下所示:
=INDEX(Sheet2!$A:$B,MATCH("*" & B5 & "*",Sheet2!$B:$B,0),1)