Vlookup with multiple results

dsrt16

Board Regular
Joined
Jun 18, 2005
Messages
208
I am creating a database with authors, book titles, descriptions, and prices. When I type in a specific authors name in cell C3 I need all the titles in the datbase to appear. I know how to get one to do so, but not all of them.

=VLOOKUP(C3,Book List!A5:D563,2,FALSE) will return one of the book titles.

I have a table set up on the Book List worksheet with author, book title, desciption, and price. I have it set up where I only type the authors name once in the first column then in the second column I list their book titles. After listing their titles then I move on to the next author like this:

Then when I enter Name of Author in cell C3 I want cell C5 to list all the books of that author. (aka A fun title, Another Title, Yeah).
However with the formula I have above it will just list the first title. (aka A fun title)

Edited by NateO: Tried to clean up the table.

Edited by NateO, again: Nope, not working. Deleted the table. You might be interested in the HTML Maker, here:

http://www.puremis.net/excel/downloads.shtml
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
NateO's code works for me until I have more than 5462 rows. How would I have ~11,000 rows - how can I make this work?

Or, if someone wants to take a shot at the whole problem:
My goal is similar to the original one here, although it's jsut the first step for me. Once I have the column with the info listed like NateO's code will output (they are dates in my case; so far I was planning to use this code once for the admit date column, and then again for the release date column), I plan to extract each to it's own cell, then get the difference between each set of admit/release dates and sum them - essentially, each name may have come through the program from 1 to 20+ times, for varying lengths each time. I want to know the total number of days they've spent here.
I can move around columns, but essentially what I have are columns with the following headings, each having data in about 11,000 rows (sorry - I can't install software on this computer, so no HTML Maker pic):
Name, Booked, Released.
These columns are in Sheet 1 of a file called JTrac File.xls; I will be referencing them from a file called MHPTR.xls.
Thank you!
M
 
Upvote 0
Well, based on your post, I think you're using XL 2000, seeing as Join() works, but you're tripping on trying to Transpose an array with more than 5461 elements.

There's not much getting around this based on how I'm doing this, so, it might be loop-time. See/try Foo2():

Code:
Public Function foo2( _
    ByRef rngIn As Range, ByRef nameIn As String) As String
Dim tmpArr() As Variant, newArr() As String
Dim i As Long, j As Long
Let tmpArr = rngIn.Value
ReDim newArr(1 To UBound(tmpArr, 1))
For i = LBound(tmpArr, 1) To UBound(tmpArr, 1)
    If tmpArr(i, 1) = nameIn Then
        Let j = j + 1
        Let newArr(j) = tmpArr(i, 2)
    End If
Next
ReDim Preserve newArr(1 To j)
Let foo2 = Join$(newArr, ", ")
End Function

I'm using Excel '03 and both worked for me on ~10,000 rows, I think Foo2() should work for you.
Book3
ABCD
1TitleName InReturn
2JoeBook1HaroldBook3, Book7, Book9, Book33
3TomBook2HaroldBook3, Book7, Book9, Book33
4HaroldBook3
5HowardBook4
6JoeBook5
7TomBook6
8HaroldBook7
9HowardBook8
10HaroldBook9
Sheet1


And, not all is lost, Foo2() might be slightly faster, actually. I didn't test them, I did but not with enough resolution to distinguish between the two, but it might be... :)
 
Upvote 0
Hi, can you transform this code to work in rows rather than in columns?
Thanks!

Public Function foo( _
ByRef rngIn As Range, ByRef nameIn As String) As String

Dim tmpArr() As Variant

With WorksheetFunction
Let tmpArr = .Transpose( _
Evaluate("" & rngIn.Columns(1).Address & "&" _
& rngIn.Columns(2).Address & ""))
Let foo = .Substitute(Join(Filter( _
tmpArr, nameIn), ", "), nameIn, vbNullString)
End With
End Function
 
Upvote 0
Hello and welcome to the board! :)

Yes, but you should use/start with foo2() for 2 reasons, first, for allowing larger ranges w/ Excel 2000. Second, my associate, Martin correctly notes that if the returned value has the same string as the lookup value in it, the returned string gets incorrectly truncated, something I failed to consider, actually. Probably remote, but possible...

Foo2() works as intended in all cases, I think, and doesn't appear to suffer any efficiency loss (probably because foo() has some expensive stuff going on it, which I knew when I wrote it...).

With that said, here's Foo3():

Code:
Public Function foo3( _
    ByRef rngIn As Range, ByRef nameIn As String) As String
Dim tmpArr() As Variant, newArr() As String
Dim i As Long, j As Long
Let tmpArr = rngIn.Value
ReDim newArr(1 To UBound(tmpArr, 2))
For i = LBound(tmpArr, 2) To UBound(tmpArr, 2)
    If tmpArr(1, i) = nameIn Then
        Let j = j + 1
        Let newArr(j) = tmpArr(2, i)
    End If
Next
ReDim Preserve newArr(1 To j)
Let foo3 = Join$(newArr, ", ")
End Function
Book1
ABCDEFGHI
1HaroldAladinJuanMOBBillAladinHaroldJonHarold
2Book1Book2Book3Book4Book5Book6Book7Book8Book9
3
4HaroldAladin
5Book1, Book7, Book9Book2, Book6
Sheet1


Again, I've given this limited thought/testing, so if something I haven't considered pops up, please post back to this thread. :)
 
Upvote 0
Data on another sheet

Hi, the code works great! However, if I refer the formula to a data set on another sheet the result is a number?....Is there a fix to allow using data set on another sheet?...Thanks!
 
Upvote 0
Hello, welcome to the board.

The following works fine for me:

Code:
Public Function foo2( _
    ByRef rngIn As Range, ByRef nameIn As String) As String
Dim tmpArr() As Variant, newArr() As String
Dim i As Long, j As Long
Let tmpArr = rngIn.Value
ReDim newArr(1 To UBound(tmpArr, 1))
For i = LBound(tmpArr, 1) To UBound(tmpArr, 1)
    If tmpArr(i, 1) = nameIn Then
        Let j = j + 1
        Let newArr(j) = tmpArr(i, 2)
    End If
Next
ReDim Preserve newArr(1 To j)
Let foo2 = Join$(newArr, ", ")
End Function
Book4
ABCD
1Haroldbook1, book33
Sheet1
Book4
ABCD
1Haroldbook1
2tombook2
3Haroldbook33
Sheet2


And you? :)
 
Upvote 0
Could this be the problem:
NateO said:
Second, my associate, Martin correctly notes that if the returned value has the same string as the lookup value in it, the returned string gets incorrectly truncated, something I failed to consider, actually. Probably remote, but possible...
if you have foo in column 1 and foo1 in column 2, 1 will be returned, due to the way I used the Substitute() function in the original UDF.

See foo2(). :)
 
Upvote 0
Return these results in a column

Hi -

I am new here, and in desperate need of help. I'm not a programmer, but got roped into making a rather complex excel workbook for my job. It's requiring more vb than i know. I'm using your previous script (foo2) as the basis for returning multiple values from a lookup table, but I can't seem to figure out how to output the values to a column of cells, each value to a column, instead of to a string in one cell. I've been searching all over to try and make it work.

by the way, your Foo2 script was a lifesaver; i tried dozens of ways to get the multiple values from one lookup thing to work, this was the only reliable one.

Please help!
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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