+ Reply to Thread
Results 1 to 9 of 9

Vlookup to return multiple results?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-03-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    81

    Vlookup to return multiple results?

    I have calculated the number of lines in a data set I want to bring back to a cell. Can't think of how to do it. Example:
    Sheet 1
    Column A / Column B \ Column C
    Location Number / Number of LOBs \ Result for Formula I'm looking for
    123 Anywhere St. / 3 \ LOB 1, LOB 2, LOB 3
    321 Nowhere St. / 3 \ LOB 2, LOB 6

    Sheet 2
    Column A \ Column B
    Location Number \ LOB #
    123 Anywhere St. \ LOB 1
    123 Anywhere St. \ LOB 2
    123 Anywhere St. \ LOB 3
    321 Nowhere St. \ LOB 2
    321 Nowhere St. \ LOB 6
    321 Nowhere St. \ LOB 2
    Last edited by cdotyii; 09-03-2011 at 05:10 PM.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Vlookup to return multiple results?

    Hello & Welcome to the Board,

    This is the best I can come up with...

    Change sheet names to meet your needs...

    Right now it is Sheet2 as the data and Sheet3 as the output

    Sub Macro1A()
    
        Dim Cell As Range
        Dim Data As Variant
        Dim Dict As Object
        Dim DstRng As Range
        Dim Key As Variant
        Dim Item As Variant
        Dim R As Long
        Dim Rng As Range
        Dim RngEnd As Range
        Dim Wks As Worksheet
        Dim i As Long
    
        Set Wks = Worksheets("Sheet2")
        Set Rng = Wks.Range("A2")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
    
        Set DstRng = Worksheets("Sheet3").Range("A1")
    
        Set Dict = CreateObject("Scripting.Dictionary")
        Dict.CompareMode = vbTextCompare
    
        For Each Cell In Rng
            Key = Trim(Cell)
            Item = Cell.Offset(0, 1)
            If Not Dict.Exists(Key) Then
                Dict.Add Key, Item
            Else
                Dict(Key) = Dict(Key) & "|" & Item
            End If
        Next Cell
    
        For Each Key In Dict.Keys
            With DstRng.Offset(R, 0)
                .Value = Key
                Data = Split(Dict(Key), "|")
                .Offset(0, 1).Resize(1, UBound(Data) + 1).Value = Data
                R = R + 1
            End With
        Next Key
        Columns("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
            Cells(i, 2).FormulaR1C1 = "=COUNTA(RC[1]:RC[100])"
        Next i
    End Sub
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    09-03-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Vlookup to return multiple results?

    OK That gets us half way!

    Now, I have a sheet with a bunch of LOBs and Multiple duplicates in each row. Is there a quick way to get rid of the duplicates and then CONCATENATE them into 1 row?

    Example:

    Result
    LXXX1 - LOB 1 - LOB 2 - LOB 2 - LOB 3 - LOB 2

    Should translate into this in a single cell

    LOB 1, LOB 2, LOB 3

    Thanks in Advance

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Vlookup to return multiple results?

    At this point it would help to see a sample workbook with before and after

  5. #5
    Registered User
    Join Date
    09-03-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Vlookup to return multiple results?

    Quote Originally Posted by jeffreybrown View Post
    At this point it would help to see a sample workbook with before and after
    I have done it using your macro then Manually sorting and CONCATENATING the results. But I would like to automate this for future changes

    How would I send (post) you a example sheet?

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Vlookup to return multiple results?


  7. #7
    Registered User
    Join Date
    09-03-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Vlookup to return multiple results?

    OK here is my example sheet

    What I want is Column AO on the "Final Budget by Lease #" sheet to fill in like it is based on the allocations for each lease.

    The allocation table is in its own sheet, then I split the cost for each lease up based on the allocations in the "Final Budget by Allocation" sheet.

    Thanks in Advance!
    Attached Files Attached Files

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Vlookup to return multiple results?

    Sorry haven't been around all day. A day filled with celebrating my grandson's third birthday...

    Anyway, not sure I've got what it takes to make this one happen so I'll ask those much more handier with VBA than myself...

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Vlookup to return multiple results?

    String Concatenation is a fun problem, but I say never reinvent the wheel. Mike Erickson published the best UDF for this long ago and I've been using it faithfully ever since.
    I've added this UDF to the attached sheet. Now you have a new function you can put in AO2 and copy down to do this concatenation for you.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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