Roman L
Roman L

Reputation: 13

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. 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...

I wanted the file to work like this:

  1. In fourth row of the first sheet (f.e. Instr_Compo) look up for value from "Location" row, somewhere in the second sheet
  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.
  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.

Probably there is something really simple I haven't tried but I'm out of ideas.

Upvotes: 0

Views: 420

Answers (1)

Xabier
Xabier

Reputation: 7735

You could achieve this using VBA like below:

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)

Upvotes: 1

Related Questions