0

This was solved using a formula.
Unfortunately, I need a solution that works in Excel 2016, and it seems VBA is the best/only route.

Legend: (this is across multiple worksheets in the same workbook)
Each column has a header.
Column A of Sheet3: List of Names
Column H of Sheet3: List of Email Addresses
Column M of Sheet1: contains the below formula dragged down, which produces a variable number of rows of data: =IFERROR(INDEX($A$2:$A$42,MATCH(0,IF("1"=$L$2:$L$42,COUNTIF($O$1:$O1,$A$2:$A$42),""),0)),"")

In column M of Sheet1, I have an Index/Match formula, which populates with a list of people's names. (As said above, the number of names that appear is ever-changing.)

I'd like to look up each name that appears in column M of Sheet1 against column A of Sheet3 then return the respective email address from column H of Sheet3.

Additionally, I'd like to separate each email address with a semicolon, as this is to populate the To field of an Outlook email.

Snapshot of what the data looks like

| A, Sheet3       | H, Sheet3                | M, Sheet1     |
| --------------- | ------------------------ | ------------- |
| John Smith      | JohnSmith@email.com      | Frank Sinatra |
| Kimberly Jones  | Kimberly@email.com       | Corey Smith   |
| Joe Montana     | JoeMontana@email.com     | Kimberly Jones|
| Dean Martin     | DeanMartin@email.com     | John Smith    |
| Corey Smith     | Corey.Smith@email.com    |               |
| Frank Sinatra   | Frank.Sinatra@email.com  |               |

In cell F2 of Sheet1, the macro would produce the below:

Frank.Sinatra@email.com; Corey.Smith@email.com; Kimberly@email.com; JohnSmith@email.com      

Worksheet tab names:
Worksheet Tabs

Worksheet1:
Worksheet1

Worksheet3:
Worksheet3

Community
  • 1
  • 1
maestro
  • 17
  • 7
  • 1
    You may be looking for a [UDF version of TEXTJOIN](https://stackoverflow.com/questions/39532189/vlookup-with-multiple-criteria-returning-values-in-one-cell). – BigBen Feb 18 '21 at 04:55
  • Oh, awesome. thanks Ben. I'll see if i can try to figure out what's going on in here and how to combine the XLookup...going to be my first time doing something like this – maestro Feb 18 '21 at 05:05

1 Answers1

1

Try,

Function JoinEmail() As String
    Dim Ws(1 To 2) As Worksheet
    Dim vDB As Variant, vR() As Variant
    Dim vName As Variant
    Dim Dic As Object  'Dictionary
    Dim i As Long, n As Integer
    Dim s As String
    
    Set Ws(1) = Sheets(1)
    Set Ws(2) = Sheets(3)
    
    Set Dic = CreateObject("Scripting.Dictionary")
    
    vDB = Ws(2).UsedRange 'Sheets(3) data
    With Ws(1)
        vName = .Range("M2", .Range("M" & Rows.Count).End(xlUp))
    End With
    
    For i = 2 To UBound(vDB, 1)
        Dic.Add vDB(i, 1), vDB(i, 8) 'name, email
    Next i
    
    For i = 1 To UBound(vName, 1)
        s = vName(i, 1)
        If Dic.Exists(s) Then
            n = n + 1
            ReDim Preserve vR(1 To n)
            vR(n) = Dic(s)
        End If
    Next i
    If n Then
        JoinEmail = Join(vR, "; ")
    Else
        JoinEmail = ""
    End If
    
End Function

enter image description here

Sheet1 image

enter image description here

Sheet3 image enter image description here

Dy.Lee
  • 7,527
  • 1
  • 12
  • 14
  • Thanks a lot for the help Dy. I'm having trouble with this. I added a couple lines, which i'll indicate in your post in bold text. feel free to delete, as i'm not sure it's correct. – maestro Feb 19 '21 at 04:20
  • I guess I can't bold my edits, because it's showing as code, so it's just showing in between asterisks. When I run the code, nothing happens, not sure why – maestro Feb 19 '21 at 04:26
  • Just type `=JoinEmail()` in your cell. – Dy.Lee Feb 19 '21 at 04:29
  • hm. i'm getting a `#NAME?` error. does it matter what cell or worksheet I enter `=JoinEmail()` in? also, thanks for reverting back to your code – maestro Feb 19 '21 at 04:49
  • nevermind, i changed the name of the module and that took away the `#NAME?` error. but now it just produces an empty cell rather than what you are showing in your screenshot...i'll try toying around with it – maestro Feb 19 '21 at 04:52
  • @maestro, If the problem persists, try uploading the images of your sheet 1 and sheet 3 so that the sheet tabs are visible. It will help you find the problem. – Dy.Lee Feb 19 '21 at 04:57
  • just uploaded them now – maestro Feb 19 '21 at 05:17
  • @maestro, In my case it works fine. I do not know the cause. The data structure seems to be the same...., I uploaded an image of my file. – Dy.Lee Feb 19 '21 at 05:46
  • Weird. Not sure what's going on. I'll spend more time with this on Sunday. Thanks so much for the help Dy, this thing is beautiful. Hopefully I'll get it working – maestro Feb 19 '21 at 06:30
  • @maestro, What is the operating system of your computer? Is it a window? – Dy.Lee Feb 19 '21 at 08:59
  • Hi Dy. yes it is Windows 10 – maestro Feb 22 '21 at 00:29
  • Created a new file with your code and began replicating my original file to identify at what point the code stops working. Looks like what is causing the error are the other macros I have in this workbook. Will have to come back to this another day. When I inserted my other macros into the workbook, I this error: "Compile error: Invalid outsider procedure" – maestro Feb 22 '21 at 02:12