Level 4 project: clean up exported data
To finish level 4, you clean up a customer export in three steps, the kind that lands in an office inbox every week: gaps, stray spaces and customers listed twice. At the end you have a clean, sorted list with a total, and blocked customers are flagged.
You need: deleting rows, sorting, Find, writing formulas and With and ScreenUpdating. Part 1 also uses Trim from worksheet functions.
The situation
An office supplies wholesaler pulls its customer list with sales figures out of the inventory system every Monday. The export is messy: blank rows between customers, names with spaces at the start or doubled in the middle, and because two branches are merged, some customers appear twice. Accounts also keeps a blocked list of customers who only get deliveries against prepayment.
Until now, someone cleaned this up by hand every Monday. The macros should do it in seconds, the same way every time. Each part is its own macro with its own exercise. In real Excel you would run them one after the other at the end.
The project
Part 1: blank rows and stray spaces
The raw export. Watch out for the two blank rows in a row and for names like "Peters Motors" with two spaces.
Task Part 1: the export from the inventory system has blank rows in the middle and spaces where none belong: at the start, at the end and doubled in the middle. The macro CleanExport deletes every row where column A is empty and removes all extra spaces from name and city (columns B and C), leaving exactly one space between two words. The numbers in A and D stay as they are.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer no | Name | City | Sales |
| 2 | 1001 | Carter Bakery | Leeds | 4,820.00 |
| 3 | ||||
| 4 | 1002 | Peters Motors | Denver | 12,500.00 |
| 5 | 1003 | Green Dental | Boston | 3,155.00 |
| 6 | ||||
| 7 | ||||
| 8 | 1004 | Harris & Sons | Bristol | 960.00 |
| 9 | 1005 | Lakeside Hotel | Keswick | 7,400.00 |
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
Part 2: duplicates, sorting, total
The list is tidy now, but some customers are in it twice. After that, the largest sales should be at the top, with a total below that updates when figures are corrected.
Task Part 2: the cleaned list contains some customers twice, because two exports were merged. Duplicate rows are completely identical. The macro FinishList removes the duplicates, then sorts the list by sales (largest at the top) and writes "Total" in column A below the last row, with a SUM formula over all sales in column D.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer no | Name | City | Sales |
| 2 | 1004 | Harris & Sons | Bristol | 960.00 |
| 3 | 1001 | Carter Bakery | Leeds | 4,820.00 |
| 4 | 1003 | Green Dental | Boston | 3,155.00 |
| 5 | 1002 | Peters Motors | Denver | 12,500.00 |
| 6 | 1004 | Harris & Sons | Bristol | 960.00 |
| 7 | 1005 | Lakeside Hotel | Keswick | 7,400.00 |
| 8 | 1001 | Carter Bakery | Leeds | 4,820.00 |
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
Part 3: flag blocked customers
Now the blocked list comes in. It also contains a number that is not in the customer list, and one that only matches part of another number.
Task Part 3: accounts keeps a Blocked sheet with customer numbers that only get deliveries against prepayment. The macro MarkBlocked looks up each number from the Blocked sheet with Find in column A of the Customers sheet (exact matches only). On a match, "blocked" goes into column E for that customer, and cells A to E of that row get the fill color RGB(255, 199, 206). If the number is not there, the macro writes "not in list" in column B of the Blocked sheet.
Customers
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Customer no | Name | City | Sales | Status |
| 2 | 1002 | Peters Motors | Denver | 12,500.00 | |
| 3 | 11004 | Abbott News | Leicester | 8,300.00 | |
| 4 | 1005 | Lakeside Hotel | Keswick | 7,400.00 | |
| 5 | 1001 | Carter Bakery | Leeds | 4,820.00 | |
| 6 | 1003 | Green Dental | Boston | 3,155.00 | |
| 7 | 1004 | Harris & Sons | Bristol | 960.00 |
Blocked
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer no | Note | ||
| 2 | 1004 | |||
| 3 | 1009 | |||
| 4 | 1002 | |||
| 5 |
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
What matters
Delete backwards
Parts 1 and 2 delete rows, so both loops run from the bottom up with Step -1. Going forwards, a second blank row directly below a first would be skipped, and of three identical customers one too many would remain.
Trim is not always Trim
The VBA function Trim only removes spaces at the start and end. The worksheet function WorksheetFunction.Trim works like TRIM in Excel and also turns several spaces between words into one. For "Peters Motors" you need the second one.
Sort first, then compare
Duplicate customer numbers are easiest to spot when they sit directly below each other. So part 2 sorts by customer number first, deletes the repeats, and only then sorts by sales. The length of the list changes between the steps, so the last row is found again.
Find exactly, and check the result
In part 3, Find searches a different sheet from the one the loop runs on. LookAt:=xlWhole stops 1004 from matching 11004, and Is Nothing catches numbers that are not in the customer list. A With block for the blocked list keeps the lines short, but every reference to the customer list then needs Worksheets("Customers") spelled out.
If it does not work
- If a blank row is left in part 1, the loop runs forwards. If double spaces remain in the middle, you are using the VBA function Trim instead of WorksheetFunction.Trim.
- If a customer is still listed twice in part 2, the sort by customer number before deleting is missing, or the loop compares with the wrong row (correct: row i with row i - 1, loop down to 3).
- If the total in part 2 lands too far down, the last row was not found again after deleting.
- If Check says a formula is missing in part 2, there is a fixed number in that cell. The total should go in with
.Formula = "=SUM(…)". - If the wrong customer is flagged in part 3,
LookAt:=xlWholeis missing. If run-time error 91 appears, the Is Nothing check is missing. - All three parts are also checked against other data, with more rows and different matches. Fixed row numbers will show up there.
What you learned in level 4
You copy ranges with and without formatting and transfer values between sheets, delete and insert rows in loops without skipping any, sort by several columns, search with Find and handle "not found" safely, write real formulas into cells, and keep long macros readable and quick with With and ScreenUpdating. That covers most of the recurring clean-up work on lists. Level 5 makes macros faster and sturdier: data in memory instead of cell by cell, lookups with the Dictionary, and proper error handling.
FAQ
How many points is the project worth?
Each of the three project tasks counts three times as much as a single exercise, because you solve several steps together.
Is the project free?
No. The projects from level 2 on are part of Premium. In every lesson of level 4 the first exercise is free, so you can prepare without Premium.
Is there only one right answer?
No. Check looks at the sheet after the macro, not at the text of your code. For or For Each, Range or Cells, with or without With: if the result is right, on the hidden test data too, it counts.
Why not just use RemoveDuplicates?
In real Excel, part 2 also works with Range("A1").CurrentRegion.RemoveDuplicates Columns:=Array(1), Header:=xlYes. This project practises the sort-and-backward-loop route because it shows how the pieces from level 4 fit together, and because you decide yourself which row stays.
How do I use this in real Excel?
Put the three macros in a module of a workbook you paste the export into, and write a fourth one that calls them in turn. How macros call other macros and pass values is covered in level 5. Save as .xlsm and try it on a copy first.