Case 1: Clean an address list with VBA
The office needs a clean mailing list for the newsletter. The data comes from three sources and looks like it. Three macros tidy it up: capitalisation and spaces, ZIP codes, duplicates.
In short: WorksheetFunction.Trim removes spaces at the edges and doubled spaces in the middle, WorksheetFunction.Proper capitalises every word. A ZIP code only keeps its leading zero if the cell is set to Text first: .NumberFormat = "@", then .Value = Format(zip, "00000"). A Dictionary with name and ZIP code as the key spots duplicates.
The situation
Part of the addresses comes from an old member list, part from a web form, and the rest was typed in by hand. Some people filled in the web form all in lower case or all in capitals, and copying left spaces at the start and end. The old list stored ZIP codes as numbers, which dropped the leading zero: 02108 Boston became 2108. And anyone who was in two sources now appears twice.
The list has four columns: Name, Street, ZIP code, City. Each task is a macro of its own for one step. In real Excel you would run them one after another.
Task 1: spaces and capitalisation
Task The mailing list for the newsletter comes from three sources and looks like it: spaces at the start, at the end and doubled in the middle, names and cities sometimes in lower case, sometimes in capitals. Write a macro CleanAddresses that goes through every row from row 2 to the last and writes name and city back without extra spaces and with a capital letter at the start of each word ("Emily Carter", "Boston"). The street only gets its spaces cleaned up, the ZIP code stays as it is.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Name | Street | ZIP code | City |
| 2 | emily carter | 14 Elm Street | 2108 | boston |
| 3 | JAMES MILLER | 220 Main Street | 6103 | HARTFORD |
| 4 | Olivia Brooks | 8 Pine Avenue | 60614 | Chicago |
| 5 | daniel HAYES | 45 Oak Road | 3801 | portsmouth |
| 6 | Sarah Collins | 3 Bridge Street | 7102 | newark |
| 7 | Ryan Foster | 18 Maple Drive | 5401 | burlington |
| 8 | LAURA BENNETT | 9 Ridge Road | 97205 | Portland |
The VBA function Trim and the worksheet function WorksheetFunction.Trim (TRIM in Excel) share a name but do different things: the VBA function only cuts off the edges, the worksheet function also shortens several spaces between words to one. More in the WorksheetFunction lesson.
| Expression | Result for " Olivia Brooks " |
|---|---|
Trim(x) | "Olivia Brooks" |
WorksheetFunction.Trim(x) | "Olivia Brooks" |
WorksheetFunction.Proper("LAURA BENNETT") | "Laura Bennett" |
There are two ways to capitalise, and they differ on apostrophes and hyphens. The PROPER worksheet function (WorksheetFunction.Proper) capitalises every letter that follows a character which is not a letter, so "o'brien" becomes "O'Brien". According to the Microsoft documentation, StrConv(text, vbProperCase) only splits words at spaces and control characters, so real Excel turns it into "O'brien". For names, PROPER is usually the better choice. Both turn "mcdonald" into "Mcdonald" and "van der berg" into "Van Der Berg", so check names like these by hand afterwards.
Task 2: ZIP codes with a leading zero
Excel stores 02108 as the number 2108 as soon as the cell has the General format. That happens when you type it, when you open a CSV file, and also when a macro writes the text "02108" into the cell. ZIP codes are not numbers you calculate with, though; they are identifiers. They belong in the cell as text. UK postcodes contain letters, so Excel keeps them as text anyway; the problem hits US ZIP codes starting with 0 (New England, New Jersey, Puerto Rico) and German postcodes in the east of the country.
Task During the import, some ZIP codes lost their leading zero: 02108 became the number 2108. Write a macro FixZipCodes that writes every ZIP code in column C (row 2 to the last) back as five-digit text, so "02108" again. To stop Excel from turning it straight back into a number, set the cell to the Text number format ("@") first. At the end, a MsgBox shows how many ZIP codes were fixed, in exactly this form: "ZIP codes fixed: " followed by the number.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Name | Street | ZIP code | City |
| 2 | Emily Carter | 14 Elm Street | 2108 | Boston |
| 3 | James Miller | 220 Main Street | 06103 | Hartford |
| 4 | Olivia Brooks | 8 Pine Avenue | 60614 | Chicago |
| 5 | Daniel Hayes | 45 Oak Road | 3801 | Portsmouth |
| 6 | Sarah Collins | 3 Bridge Street | 7102 | Newark |
| 7 | Ryan Foster | 18 Maple Drive | 05401 | Burlington |
| 8 | Laura Bennett | 9 Ridge Road | 97205 | Portland |
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.
Without NumberFormat = "@" the exercise reports: "C2 contains 2108, expected: "02108". 6 more cells are different as well." The text from Format was right, but Excel turned it back into a number when it was written. A number format such as "00000" on the cell would display 02108, but 2108 would still be stored, and the zero would be gone again in a mail merge or a CSV export.
Task 3: remove duplicates
What counts as a duplicate is the office's decision, not the macro's. Here it is the same name and the same ZIP code. James Miller in Hartford and James Miller in Chicago are two different people; James Miller twice in 06103 Hartford is one, even if the house number is 220 once and 224 the other time. Someone will check the right address by phone later; the macro keeps the upper one.
Task The list is clean now, but some people appear more than once because they were in two sources. A row is a duplicate if the same name and ZIP code already appear in a row further up (the house number may differ; the upper address wins). The same name with a different ZIP code is a different person. Write a macro RemoveDuplicateContacts that deletes all duplicates and keeps the first row of each person. Then a MsgBox shows "Duplicates removed: " followed by the number.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Name | Street | ZIP code | City |
| 2 | Emily Carter | 14 Elm Street | 02108 | Boston |
| 3 | James Miller | 220 Main Street | 06103 | Hartford |
| 4 | Olivia Brooks | 8 Pine Avenue | 60614 | Chicago |
| 5 | Emily Carter | 14 Elm Street | 02108 | Boston |
| 6 | Daniel Hayes | 45 Oak Road | 03801 | Portsmouth |
| 7 | James Miller | 224 Main Street | 06103 | Hartford |
| 8 | Sarah Collins | 3 Bridge Street | 07102 | Newark |
| 9 | James Miller | 71 Lake Drive | 60614 | Chicago |
| 10 | Ryan Foster | 18 Maple Drive | 05401 | Burlington |
| 11 | Emily Carter | 14 Elm Street | 02108 | Boston |
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 Dictionary remembers each person the first time they appear. The loop runs from top to bottom because the first row should stay. When a row is deleted, the next one moves up, so the row number only moves on when nothing was deleted. That works with Do While, not with a fixed For loop.
Common mistakes
VBA Trim instead of the worksheet function
With Trim the spaces in the middle stay. The exercise reports: "A4 contains "Olivia Brooks", expected: "Olivia Brooks". 3 more cells are different as well."
Deleting backwards
Deleting backwards is usually the safe method (see Delete rows). Here, though, it keeps the lowest row of each person instead of the top one, and the list ends up in a different order. The exercise reports: "A2 contains "Olivia Brooks", expected: "Emily Carter". 27 more cells are different as well."
Spaces in the key
If the duplicate check runs before the cleanup, "Emily Carter" and "Emily Carter " are two different keys. That is why the order matters: tasks 1 and 2 first, then task 3.
In real Excel
For task 3, the Data tab has Remove Duplicates, and VBA can do the same with Range("A1").CurrentRegion.RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes, which also keeps the first occurrence. The Dictionary route pays off when you want more than deleting: writing the duplicates to a sheet of their own, counting them, or normalising names before comparing. Always work on a copy of the list; Ctrl+Z does not bring back rows deleted by a macro.
Check your result
- Task 1: all seven addresses are clean, for example "Daniel Hayes", "45 Oak Road", "Portsmouth".
- Task 2: three ZIP codes get a zero; "06103" and "05401" were already stored as text.
- Task 3: three duplicates go, seven addresses stay, and James Miller appears twice, once in Hartford and once in Chicago.
Back to the course overview. More on text in VBA in the string functions lesson.