Case 2: Merge lists with VBA
Two lists, one shared number, but a different order and not the same entries: the most common matching job in any office. You bring the CRM contacts over to the customer master data, first with Find, then with a Dictionary in both directions.
In short: for each customer, contacts.Columns(1).Find(What:=no, LookAt:=xlWhole) finds the row on the other sheet, If hit Is Nothing Then catches missing ones, and hit.Offset(0, 1) fetches the neighbouring columns. For large lists, or to match in both directions, a Dictionary is faster. There, the number 10017 and the text "10017" are two different keys.
The situation
Accounts keep the customer master data with customer number, company and city. Sales keep contact names and email addresses in a CRM. For an email to all customers, both lists need to come together: every customer gets their contact and email. Customers without a contact in the CRM should stand out so sales can fill the gap. And contacts whose customer number does not exist in the master data are either typos or former customers; either way, someone needs to check.
The workbook has two sheets. Customers holds customer no, company, city and the empty columns Contact and Email. Contacts holds customer no, contact, email and a Note column.
Task 1: get contacts with Find
Task The Customers sheet holds the customer master data from accounts, the Contacts sheet an export from the CRM with contact name and email, in a different order and not for every customer. Write a macro GetContacts that looks up each customer number in column A of Contacts and writes the contact name and email into columns D and E of Customers. If there is no contact, column D says "missing" and column E stays empty.
Customers
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Customer no | Company | City | Contact | |
| 2 | 10017 | Carter Bakery | Leeds | ||
| 3 | 10023 | Peters Motors | Denver | ||
| 4 | 10031 | Green Garden Center | Bristol | ||
| 5 | 10042 | Lakeside Hotel | Keswick | ||
| 6 | 10058 | Hill Street Dental | Boston | ||
| 7 | 10064 | Miller Print | Chicago | ||
| 8 | 10079 | Roth Legal | York |
Contacts
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer no | Contact | Note | |
| 2 | 10058 | Julia Grant | j.grant@example.com | |
| 3 | 10017 | Tom Carter | t.carter@example.com | |
| 4 | 10031 | Amy Green | a.green@example.com | |
| 5 | 10090 | Steve Allen | s.allen@example.com | |
| 6 | 10064 | Ralph Miller | r.miller@example.com | |
| 7 | 10023 | Nina Peters | n.peters@example.com |
Find works like searching with Ctrl+F and returns the cell holding the number. Two points from the Find lesson matter: LookAt:=xlWhole, so that 1001 does not match inside 10017, and the Is Nothing check. Without it, the macro stops at the first customer without a contact, and the exercise reports: "Run-time error '91': Object variable or With block variable not set". From the cell it found, Offset fetches contact and email from the same row.
Task 2: match both ways with a Dictionary
Now the CRM export arrives as a CSV file. Excel has taken the customer numbers in as text, some with a trailing space. On screen it looks the same, but for a Dictionary it makes all the difference:
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
d.Add "10017", 3
Debug.Print d.Exists(10017) ' False: number vs text
Debug.Print d.Exists(CStr(10017)) ' True
Debug.Print d.Exists("10017 ") ' False: trailing space
Task This time the contact list comes as a CSV export: the customer numbers are stored as text, some with a trailing space. Write a macro MatchLists with two Dictionaries that matches in both directions. 1. As in task 1, every customer gets contact name and email in D and E, or "missing" in D. 2. Every contact whose number does not exist on the Customers sheet gets the note "not a customer" in column D of the Contacts sheet.
Customers
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Customer no | Company | City | Contact | |
| 2 | 10017 | Carter Bakery | Leeds | ||
| 3 | 10023 | Peters Motors | Denver | ||
| 4 | 10031 | Green Garden Center | Bristol | ||
| 5 | 10042 | Lakeside Hotel | Keswick | ||
| 6 | 10058 | Hill Street Dental | Boston | ||
| 7 | 10064 | Miller Print | Chicago | ||
| 8 | 10079 | Roth Legal | York |
Contacts
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer no | Contact | Note | |
| 2 | 10058 | Julia Grant | j.grant@example.com | |
| 3 | 10017 | Tom Carter | t.carter@example.com | |
| 4 | 10031 | Amy Green | a.green@example.com | |
| 5 | 10090 | Steve Allen | s.allen@example.com | |
| 6 | 10064 | Ralph Miller | r.miller@example.com | |
| 7 | 10023 | Nina Peters | n.peters@example.com |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
The macro first builds a Dictionary of the contacts (number as text without spaces, value = row). Then it goes through the customers once, fetches the data and records every customer number in a second Dictionary. Finally it uses that second Dictionary to find contacts without a customer. Each list is read once or twice, instead of searching the whole other list for every customer. With a few dozen rows you won't notice; with twenty thousand you will. More in the Dictionary and Collection lesson.
Common mistakes
Number vs text
Without CStr on the customer side, the Dictionary finds no contact at all. The exercise reports: "Customers!D2 contains "missing", expected: "Tom Carter". 14 more cells are different as well."
Forgetting Trim
With CStr but without Trim on the export, only the customer whose number has a trailing space in the CSV goes missing. The exercise then reports "Customers!D2 contains "missing", expected: "Tom Carter". 1 more cell is different as well."; the second difference is the wrong "not a customer" note for that contact.
Using the hit without checking
Calling hit.Offset in task 1 without checking for Nothing first ends in run-time error 91 at the first customer without a contact.
In real Excel
For a one-off match, an XLOOKUP or VLOOKUP formula is often enough. A macro pays off when the match comes every week with a new export, or when you want to flag both directions as here. Whenever you compare numbers from a CSV file, make both sides alike first, for example with CStr and Trim. It is the most common reason why a match "finds nothing" although the numbers look identical on screen.
Check your result
- Task 1: five customers get a contact; Lakeside Hotel and Roth Legal say "missing".
- Task 2: as in task 1, and on the Contacts sheet number 10090 gets the note "not a customer". Tom Carter is found despite the space in the export.
Back to case 1 or to the course overview.