Results 1 to 10 of 49

Thread: LookUp Value and Concatenate All Found Results

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by herman925 View Post
    I do have one need from this code though, that is, to add 'and' for the last two sets to variables looked up by the code.
    Here is the code where I replaced the last line of code before the last "End If" with two lines of code...
    Code:
    Function LookUpConcat(ByVal SearchString As String, _ SearchRange As Range, _ ReturnRange As Range, _ Optional Delimiter As String = " ", _ Optional MatchWhole As Boolean = True, _ Optional UniqueOnly As Boolean = False, _ Optional MatchCase As Boolean = False) Dim X As Long, CellVal As String, ReturnVal As String, Result As String If (SearchRange.Rows.Count > 1 And SearchRange.Columns.Count > 1) Or _ (ReturnRange.Rows.Count > 1 And ReturnRange.Columns.Count > 1) Then LookUpConcat = CVErr(xlErrRef) Else If Not MatchCase Then SearchString = UCase(SearchString) For X = 1 To SearchRange.Count If MatchCase Then CellVal = SearchRange(X).Value Else CellVal = UCase(SearchRange(X).Value) End If ReturnVal = ReturnRange(X).Value If MatchWhole And CellVal = SearchString Then If UniqueOnly And InStr(Result & Delimiter, Delimiter & _ ReturnVal & Delimiter) > 0 Then GoTo Continue Result = Result & Delimiter & ReturnVal ElseIf Not MatchWhole And CellVal Like "*" & SearchString & "*" Then If UniqueOnly And InStr(Result & Delimiter, Delimiter & _ ReturnVal & Delimiter) > 0 Then GoTo Continue Result = Result & Delimiter & ReturnVal End If Continue: Next Result = Mid(Result, Len(Delimiter) + 1) LookUpConcat = Application.Replace(Result, InStrRev(Result, Delimiter), Len(Delimiter), " and ") End If End Function

  2. #2
    Junior Member
    Join Date
    Sep 2016
    Posts
    3
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    Here is the code where I replaced the last line of code before the last "End If" with two lines of code...
    Code:
    Function LookUpConcat(ByVal SearchString As String, _ SearchRange As Range, _ ReturnRange As Range, _ Optional Delimiter As String = " ", _ Optional MatchWhole As Boolean = True, _ Optional UniqueOnly As Boolean = False, _ Optional MatchCase As Boolean = False) Dim X As Long, CellVal As String, ReturnVal As String, Result As String If (SearchRange.Rows.Count > 1 And SearchRange.Columns.Count > 1) Or _ (ReturnRange.Rows.Count > 1 And ReturnRange.Columns.Count > 1) Then LookUpConcat = CVErr(xlErrRef) Else If Not MatchCase Then SearchString = UCase(SearchString) For X = 1 To SearchRange.Count If MatchCase Then CellVal = SearchRange(X).Value Else CellVal = UCase(SearchRange(X).Value) End If ReturnVal = ReturnRange(X).Value If MatchWhole And CellVal = SearchString Then If UniqueOnly And InStr(Result & Delimiter, Delimiter & _ ReturnVal & Delimiter) > 0 Then GoTo Continue Result = Result & Delimiter & ReturnVal ElseIf Not MatchWhole And CellVal Like "*" & SearchString & "*" Then If UniqueOnly And InStr(Result & Delimiter, Delimiter & _ ReturnVal & Delimiter) > 0 Then GoTo Continue Result = Result & Delimiter & ReturnVal End If Continue: Next Result = Mid(Result, Len(Delimiter) + 1) LookUpConcat = Application.Replace(Result, InStrRev(Result, Delimiter), Len(Delimiter), " and ") End If End Function
    Hi Rick, thanks for the help. I do still have some trouble with this code.

    It will produce the word 'and' for even cells that only contain 1 name.

    So for my example I gave previously, it worked well. However for cells that only contains 1 name, for example 'Rick Rothstein', it will become 'Rick and Rothstein'

    Is it possible to make the rule (I'm, completely, a noob, on VB) such that for cells containing more than 1 value, the final 2 values can be separated by 'and' while others by comma only.

    so:
    (for 1 value) James Jones
    (for 2 values) James Jones and Lillian Jones
    (for 3 values) James Jones, Lillian Jones and Michael Jones

    Sorry for the trouble, thank you.

  3. #3
    Junior Member
    Join Date
    Oct 2016
    Posts
    1
    Rep Power
    0
    Hi Rick,

    Firstly, thanks for sharing this helpful code.

    I have two questions, so I appreciate if you can help me.

    1. Is it possible to skip duplicate results?

    2. I have some empty cells; is it possible to skip them in the result.

    Rick.jpg

  4. #4
    Junior Member
    Join Date
    Oct 2016
    Posts
    2
    Rep Power
    0
    Hi Rick,

    I saved my workbook as macro enabled and saved your UDF code as a module. However when I enter the formula into my table I get the following error: Compile Error. Unexpected end function.

    Is there a way to trouble shoot this?

    Capture.jpg


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg
    https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG
    https://www.youtube.com/watch?v=vXyMScSbhk4&lc=Ugxa2VYHMWJWXA6QI294AaABAg. 9irLgSdeU3r9itU7zdnWHw
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg.9htChVuaX9W9htG01cKBzX
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg. 9ht16tzryC49htJ6TpIOXR
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgwMKwGZpDjv7vi7pCx4AaABAg
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg. 9ht16tzryC49htOKs4jh3M
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgxVW-am20rQ5GFuJ9F4AaABAg
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 10-24-2023 at 02:54 PM.

  5. #5
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by rwoodsum View Post
    I saved my workbook as macro enabled and saved your UDF code as a module. However when I enter the formula into my table I get the following error: Compile Error. Unexpected end function.

    Is there a way to trouble shoot this?

    Capture.jpg
    It is hard to see that small picture, but if what is displayed in the picture is all of the code you copied, then you did not manage to copy all of the code there is.

  6. #6
    Junior Member
    Join Date
    Oct 2016
    Posts
    2
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    It is hard to see that small picture, but if what is displayed in the picture is all of the code you copied, then you did not manage to copy all of the code there is.
    Oh gosh I didn't even see that the original code has a scroll bar

Similar Threads

  1. Replies: 5
    Last Post: 06-04-2013, 01:04 PM
  2. Replies: 6
    Last Post: 12-12-2012, 08:03 PM
  3. Lookup lookup lookup just can't make it work
    By work2live in forum Excel Help
    Replies: 1
    Last Post: 12-08-2012, 11:48 PM
  4. Excel found unreadable content
    By zyousafi in forum Excel Help
    Replies: 2
    Last Post: 08-08-2012, 10:41 AM
  5. Concatenate Multiple Lookup Values in Single Cell
    By Admin in forum Download Center
    Replies: 4
    Last Post: 04-06-2012, 09:07 PM

Tags for this Thread

Posting Permissions

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