+ Reply to Thread
Results 1 to 8 of 8

Foreach loop lookup between two worksheets

  1. #1
    Registered User
    Join Date
    04-20-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    43

    Foreach loop lookup between two worksheets

    Hi All,

    I'm kind of stuck trying to fix a loopup between two worksheets.

    Column P of sheet 1, contains multiple references to a Sheet called TOM,

    So column P would look something like TOM001.001, TOM002.007

    What I need to achieve is for each value in Column P (which corresponds to Column A of worksheet TOM), copy the value in TOM Column B, to Sheet1 column Q comma separated.

    A vlookup got it to do it when column P only had one value, but I can't figure out how to do it iteratively.
    =VLOOKUP(P2,'TOM'!$A1:$B97,2,FALSE)

    If I can get this to work, I might need to add another mapping between TOM column C and Sheet 1 column R

    Anyone able to help?

    Many thanks
    Asparak

  2. #2
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Foreach loop lookup between two worksheets

    Please post an example workbook using fictional / non confidential data that accuartely represents your real one, showing the existing data and the expected results.

    If I'm following your question correctly then I don't see any easy way to do what you want without the use of vba.

  3. #3
    Registered User
    Join Date
    04-20-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Foreach loop lookup between two worksheets

    Thanks for the offer. My VB and VBA is very rusty. Been 15 years since I needed to touch it. Mock up file attached.
    Attached Files Attached Files
    Last edited by Cutter; 07-13-2012 at 08:30 PM. Reason: Removed whole post quote

  4. #4
    Registered User
    Join Date
    04-20-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Foreach loop lookup between two worksheets

    Made a little progress, by using a series of =MID($P125,1,10) etc statments, then a =IFERROR(VLOOKUP($R125,'TOM'!$A2:$B110,2,FALSE),"") and finally concatenating all the outputs back together again, but it's already a maintenance nightmare doing this. The largest so far is 10 sets of MID and VLOOKUP for a single row. Anyone any brighter ideas?

  5. #5
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Foreach loop lookup between two worksheets

    Looking at your dummy file, the long winded method you've tried is about the only non-vba way of completing the task

    I've been trying to put together what should be a simple udf to split the references in column P, perform a lookup on each string, then concatenate the results.

    At the moment it's returning errors not results, I'm not going to be defeated, but don't think I'm going to get a working version posted today.

  6. #6
    Registered User
    Join Date
    04-20-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Foreach loop lookup between two worksheets

    Really appreciate you trying to help. I'm also glad I haven't missed an obvious way of doing this. I did look at doing this as a single nested statement as well, but that I believe will ause me more of a maintenance headache as it would be almost impossible to spot which entry had caused it to fail.

  7. #7
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Foreach loop lookup between two worksheets

    Copy this code to a standard module (in excel press Alt + F11 to open the editor, then insert > module from the editor menu).

    Please Login or Register  to view this content.
    Then in Q2 use =MLOOKUP(P2,TOM!$A$4:$B$13,2)

    Formula entry is the same as VLOOKUP, but without the last optional TRUE /FALSE argument, I've fixed that to false / exact match within the code.

    If no match is found the formula will return an error which can be suppressed in the usual way, i.e. =IFERROR(MLOOKUP(P2,TOM!$A$4:$B$13,2),"")

    Where a cell contains multiple strings to lookup, i.e. P3 in the dummy file, the function will return the whole set as a single error.

  8. #8
    Registered User
    Join Date
    04-20-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Foreach loop lookup between two worksheets

    Thank you so much. Really appreciate your time. I'll try that out and let you know.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1