Link to home
Start Free TrialLog in
Avatar of Conor Newman
Conor NewmanFlag for Ireland

asked on

EXcel VBA: Retrieving Contact Details by name, where 2 entries may have same name

Hi guys, I have a transmittal system, that automatically emails transmittal information out to appropriate users. It gets the users names from a  list specific to the active project, then looks that user up in a contacts matrix that lists all contacts across all projects.

My issue is that there may come a time when 2 contacts in that list have the same name. The contacts list contains their names, email, phone, employer and a unique id number for each entry.

I need my code to go away look at this contacts matrix and find a user by name to retrieve his email, this I have working, but, I want it to FIRST, check if there are multiple entries for this name and if so prompt the user to select which is the contact they want via a drop down listing the two identical user names plus their company name.


The code I'm using to get the Transmittal sheet go and find the contact details is as follows:
Sheets("Transmittal").Select

' Internal Distribution

Workbooks.Open Filename:="\\xxxxxxxx\Projects\_Project Contacts Matrix\Projects Contacts Matrix.xlsm" 'Contacts list with full details for everyone

If Not myDict Is Nothing Then
    myDict.RemoveAll
Else
    Set myDict = CreateObject("Scripting.Dictionary")
End If

For Each r In Workbooks("Transmittal Log.xlsm").Sheets("Transmittal").Range("A9:A25") 'Lists the Full names of the contacts I need to email to'
    If r.Value <> "" Then
        If Not myDict.exists(r.Value) Then
        myDict.Add r.Value, i
        i = i + 1
        Workbooks("Projects Contacts Matrix.xlsm").Activate 'the workbook containing the full contact details'
            Range("G:G").Select      'Column where full name is found'
            Set RangeToSearch = Selection
            Set FoundCell = RangeToSearch.Find(r.Value)
                If Not FoundCell Is Nothing Then
                iDistRow = RangeToSearch.Find(r.Value).Row
                Else
                MsgBox "No Contact Details found for " & r.Value & ". Mail Not Sent"
                Exit Sub
                End If
        Distrib2 = Distrib2 & ";" & Range("D" & iDistRow).Value 'Add the email address of that contact'
        Workbooks("Transmittal Log.xlsm").Activate
        End If
    End If
Next r

myDict.RemoveAll
Set myDict = Nothing

Open in new window



As you can see, with the search function, I'll by default just get the first entry with that name. I need if there are 2 entries with the same name have them both pop up (showing name and Company name [ column C] with a select option for the user to decide which on he wants.

Any idea's? Have I given enough information for anyone to figure it out?
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

You are using the fullname as your index in the dictionary, you could modify for example:


        If Not myDict.exists(r.Value) Then

as is for the not found case and in the found case modify the value by adding a "|" character as an example followed by the next set of data ... and then split the values using the "|" to establish how many entries are associated with a name.

Chris
Ignore my alternative .. as I view your code a bit further I see the issue is that the data is located in another workbook using the same key data so it isn't so straightforward.

Chris
Avatar of Conor Newman

ASKER

Yeah, it's a bit of a pain.

Think maybe I need to search through the range("G:G") in the contacts list workbook for my r.value, and add each entry matching that name to an array also taking their company name, using a count integer to count how many entries there is, if the count integer is greater than 1, then pass the array into a combobox in a pop up, and let the user select which one, if the count integer = 1, then just use that persons email.

Problem is I'm not sure how to go about this...
Two thoughts:
if your contacts data is sorted you could just look ahead after a successful find to see if the name in the next row is the same - then you could pop up your question
If the data is not sorted, you could add a new column to the contacts data using countif to count the number of occurrences of that name in the whole list.  Then you could query this to see if there were more than one entry -
for example in col H you could have something like
=COUNTIF($G$3:$G$350,A3)

Open in new window

- this would be 1 in most cases, higher where there were duplicates
Hi Andrew,

Identifying if there is a duplicate isn't the issue, having a pop up in the other workbook, allowing the user to select which of the users to use in the case there is a duplicate is the issue.
OK - so you want the VBA code to create a userform displaying the duplicates and returning the selected one?  That's doable, but I'm at work at the moment and is's more than a 2 minute job.  I'll do something this evening if someone else hasn't jumped in for you by then.
Yes, I have no problem creating userforms, but, getting the code from existing macro to create one and pass the data from the duplicates into a combobox on the UserForm, then take back the data the users selects as a variable that I can use to move on with.

Not sure at all how to do that lol.
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually works perfectly Chris. Thank you! A few minor edits to include the Contacts Company name etc and it was exactly what I needed. Nice idea with the numbered options instead of the hassle of creating a userform. Very practical, and still uses the search rather than a scroll through every value in a range, so it's still fast and efficient to run.

All in all, vert very nice. :)
:o)

Glad it helps, and thank you very much for the kind comments.

Chris