Lookup across multiple columns with lists in cells!!!

sxhall

Board Regular
Joined
Jan 5, 2005
Messages
192
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a range of data across three columns that contain numbers separated by commas. To left I have list of employee numbers that I need to reference back to when an instance of a number is found. To compound this i need to return all employee numbers when an instance of a number is found!!!

To hopefully make it easier to understand I have attempted an example...

Emp_______l__List_l__List__l_List__l__
Number____l__One_l__Two_l Three_l__

1112______l__1,2_ l_8_____l_7____ l__
1113______l__2___l_7_____l_6,5___ l__
1114______l__4___l_2_____l_8_____l__
1115______l__1,5_ l_9,10__l_11____l__


So for example if I typed 2 in to cell A1 in cell B1 it would return '1112,1113,1114' , a 7 would return '1112,1113' and 10 would only return '1115'

Not adverse to having another table in the spreadsheet to assist in getting the result as well. I have been at this for several hours now and just can't figure a way to get it work with additional tables and formulas combined together and am at the point of frustration!!!!

Thanks in advance for any help.

Steven

PS apologies for the crude example, but I don't know how to attach a proper example to a post.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello are there only three lists as you show (List one, two and three)?

And how many employee numbers can be returned in cell B1. If there are many, putting them all in one cell, separated by commas doesn't sem to be the most efficent way. But if that is what you need, let me know.
 
Upvote 0
Hello are there only three lists as you show (List one, two and three)?

And how many employee numbers can be returned in cell B1. If there are many, putting them all in one cell, separated by commas doesn't sem to be the most efficent way. But if that is what you need, let me know.


Hi Joyner,

Thanks for looking at this.

I currently only have three lists against the employee numbers that need to be searched through. And I doubt it will change, given the criteria they represent, to more than that so could be restricted to cover only three lists.

The list of employee numbers will only ever have one employee number contained within a cell, although it may be duplicated within the list. However it will only need to be returned once in the result if matched to a number in any of the lists.

I have tried to split out the lists (list one, two & three) in to several columns but not knowing how many numbers will be contained in the data in the first instance means I would be guessing at the number of additional columns required. Would also mean adding in more formulas that will not return a value in the majority of cases as well.

If I can offer any more detail let me know.

Steven
 
Upvote 0
Sorry it took to long try this UDF

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the UDF type =UDF name.

UDF name here is ArrayLookup
you need to provide 2 parameters
1. value you want to search
2. range in which you want to look for values (do not include the EMP numbers in it)

Code:
Public Function ArrayLookup(LookUp_Value As String, LookUp_Range As Range) As String

Dim varTest As Variant
Dim iCount As Integer
Dim irow As Integer
Dim srow As Long, erow As Long, scol As Long, ecol As Long, x As Long
Dim myStr As String, strEMP As String

srow = LookUp_Range.Rows(1).Row
erow = LookUp_Range.Rows.Count + srow - 1
scol = LookUp_Range.Columns(1).Column
ecol = LookUp_Range.Columns.Count + scol - 1
strEMP = Empty

For irow = srow To erow
    myStr = Empty
    For Each varTest In Range(Cells(irow, scol), Cells(irow, ecol))
        If myStr = Empty Then
            myStr = "|" & Trim(varTest) & "|"
        Else
            myStr = myStr & Trim(varTest) & "|"
        End If
    Next varTest
    
    myStr = Replace(myStr, ",", "|")
    myStr = Replace(myStr, " ", "")
    
    If myStr Like "*|" & Cells(1, 1) & "|*" Then
        If strEMP = Empty Then
            strEMP = Cells(irow, scol - 1)
        Else
            strEMP = strEMP & ", " & Cells(irow, scol - 1)
        End If
    End If
Next irow

ArrayLookup = strEMP

End Function


Excel 2010
ABCD
121112, 1113, 1114
2
3Emp NumberList OneList TwoList Three
411121,287
51113276,5
61114428
711151,59,1011
Sheet1
Cell Formulas
RangeFormula
B1=ArrayLookup($A$1,B4:D7)
 
Last edited:
Upvote 0
Sorry it took to long try this UDF

That was not long at all skorpionkz, I'd been stuck on it for hours!

This worked excellent given my sample data however in my example I simplified it in the fact that cell A1 would be used and where the ranges sat. When I transferred in to my actual workbook with the real data in it produced an odd result as my search cell was U16 and the range is AH9:AJ52.

Can the UDF be changed to work on a random entry of lookup cell v range? Only as I change the worksheet (as I know it will over time) the lookup up cell will move in relation the lookup range.

Also I will need to be able to list all of the values to be searched for in a column and drag this UDF down against them all to return results...

Criteria__lEmployee Numbers
1________l1112,1115_______
2________l1110 ___________
3________l _______________
4________l1176,1112_______

As an example

Thanks again

Steven
 
Upvote 0
try this updated UDF.

you need to provide 3 parameters here:
1. value you want to search
2. range in which you want to look for values (do not include the EMP numbers in it)
3. range which contains results value

NOTE: the number of rows in (2) and (3) HAS to be same!!!

Code:
Public Function ArrayLookup(LookUp_Value As String, LookUp_Range As Range, LookUp_Results As Range) As String


Dim varTest As Variant
Dim iCount As Integer
Dim irow As Integer
Dim srow As Long, erow As Long, scol As Long, ecol As Long, rcol As Long, x As Long
Dim myStr As String, strEMP As String


If Not LookUp_Range.Rows.Count = LookUp_Results.Rows.Count _
And Not LookUp_Range.Rows(1).Row = LookUp_Results.Rows(1).Row Then
    ArrayLookup = CVErr(xlErrRef)
    Exit Function
End If


srow = LookUp_Range.Rows(1).Row
erow = LookUp_Range.Rows.Count + srow - 1
scol = LookUp_Range.Columns(1).Column
ecol = LookUp_Range.Columns.Count + scol - 1
rcol = LookUp_Results.Column
strEMP = Empty


For irow = srow To erow
    myStr = Empty
    For Each varTest In Range(Cells(irow, scol), Cells(irow, ecol))
        If myStr = Empty Then
            myStr = "|" & Trim(varTest) & "|"
        Else
            myStr = myStr & Trim(varTest) & "|"
        End If
    Next varTest
    
    myStr = Replace(myStr, ",", "|")
    myStr = Replace(myStr, " ", "")
    
    If myStr Like "*|" & LookUp_Value & "|*" Then
        If strEMP = Empty Then
            strEMP = Cells(irow, rcol)
        Else
            strEMP = strEMP & ", " & Cells(irow, rcol)
        End If
    End If
Next irow


ArrayLookup = strEMP


End Function


Excel 2010
ABCD
1Emp NumberList OneList TwoList Three
211121,287
31113276,5
41114428
511151,59,1011
Sheet1



Excel 2010
AB
1411112, 1115
1521112, 1113, 1114
163
1741114
1851113, 1115
1961113
2071112, 1113
2181112, 1114
2291115
23101115
24111115
2512
2613
Sheet1
Cell Formulas
RangeFormula
B14=ArrayLookup(A14,$B$2:$D$5,$A$2:$A$5)
 
Upvote 0
try this updated UDF.

you need to provide 3 parameters here:
1. value you want to search
2. range in which you want to look for values (do not include the EMP numbers in it)
3. range which contains results value

NOTE: the number of rows in (2) and (3) HAS to be same!!!

Superb! Exactly what I am after.

Thank you very much

Steven
 
Upvote 0
Andrew,

Another problem i found is that the formula is not always refreshing when I change the search criteria! I am having to go in to each cell and then click return for the cell to recalculate. I have excel set to auto calculate and using F9 as a prompt is not working either.

Any clues as to what may be stopping it from refreshing the formula's result when I change the search criteria?

Thanks

Steven
 
Upvote 0

Forum statistics

Threads
1,215,344
Messages
6,124,407
Members
449,157
Latest member
mytux

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top