Level 2 project: commission list

To wrap up level 2, you build the monthly commission run for a sales team with macros: the rate from the tiers, each rep's commission, a total row under the list and a bonus for the highest sales. The list is a different length every month, so every macro has to find the last row on its own.

You will need: For Next, find the last row, Select Case, Offset and MsgBox from MsgBox and InputBox. For Each and Do While are other routes that work too.

The brief

An office supplies wholesaler pays its field sales reps commission on their monthly sales. The tiers are simple: under 20,000 in sales earns 2%, from 20,000 it is 3%, and from 50,000 it is 5% on the whole amount. Whoever has the highest sales in the month also gets a bonus.

Until now, the sales coordinator has worked this out by hand every month. The list comes as an export from the order system, sometimes with eight names, sometimes with twelve. A set of macros should take over, working for any length of list and getting the tier boundaries right.

The project

Part 1: rate and commission per rep

For every row down to the last one, a Select Case picks the rate based on the sales in column B. Rate and commission go into columns C and D. Mind the boundaries: exactly 20,000 and exactly 50,000 already belong to the higher tier.

Task Part 1: The macro CalculateCommission should go through every sales rep down to the last row, write the commission rate from the tiers into column C (sales under 20,000: 0.02, from 20,000: 0.03, from 50,000: 0.05) and the commission into column D, which is sales times rate. Use Select Case for the tiers.

ABCDEFG
1Sales repSalesRateCommissionTierRate
2Sarah Mitchell62,400.00under 20,0002%
3Tom Baker18,750.0020,000 to under 50,0003%
4Priya Nair35,200.0050,000 and over5%
5Chris Walker50,000.00
6Hannah Lewis27,900.00
7Mark Davies9,500.00
8Jessica Ortiz48,300.00
9Ben Carter20,000.00
10
11

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: total row and message

The commissions are now on the list. Below it goes a total row with sales and commission, then a message with the total commission for the accounts team.

Task Part 2: The commissions are now in the list. The macro AddTotals should add a total row directly below the last sales rep: the text Total in column A, the sum of sales in column B and the sum of commissions in column D. Then it reports with MsgBox, exactly: Total commission: 10127 (the number being the calculated sum).

ABCD
1Sales repSalesRateCommission
2Sarah Mitchell62,400.005%3,120.00
3Tom Baker18,750.002%375.00
4Priya Nair35,200.003%1,056.00
5Chris Walker50,000.005%2,500.00
6Hannah Lewis27,900.003%837.00
7Mark Davies9,500.002%190.00
8Jessica Ortiz48,300.003%1,449.00
9Ben Carter20,000.003%600.00
10
11
12

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: bonus for the top seller

Finally, find the month's best performer. The macro marks the bonus in column E and reports who gets it.

Task Part 3: Whoever has the highest sales gets a bonus. The macro AwardBonus should find that person, write the word Bonus in column E of their row and then report, exactly like this: Bonus goes to Sarah Mitchell (with the name from the list).

ABCDE
1Sales repSalesRateCommissionBonus
2Sarah Mitchell62,400.005%3,120.00
3Tom Baker18,750.002%375.00
4Priya Nair35,200.003%1,056.00
5Chris Walker50,000.005%2,500.00
6Hannah Lewis27,900.003%837.00
7Mark Davies9,500.002%190.00
8Jessica Ortiz48,300.003%1,449.00
9Ben Carter20,000.003%600.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

What matters

Always find the last row

All three parts are also checked against a longer list. A loop For i = 2 To 9 or a total row in Range("A10") only works for the month you can see. Find the last row at the start of each macro with Cells(Rows.Count, 1).End(xlUp).Row and work with that number.

Tiers from the top down

With Case Is >= 50000 first, then Case Is >= 20000 and Case Else, each tier needs only one limit and there are no gaps. Bands such as Case 0 To 19999 let figures with decimals slip through: 19,999.50 matches neither 0 To 19999 nor 20000 To 49999. Check tests exactly that kind of value.

Calculate first, then write

In part 2 the loop collects both totals, and writing happens afterwards. Cells(lastRow, 1).Offset(1, 0) puts the total row right below the last name. If you look up the last row again after writing, you get the total row.

Keep the row, not just the value

For the bonus you need the name and the target cell at the end. So store the row of the best rep so far in a variable and compare each further figure with Cells(best, 2). The variable starts at 2, the first data row.

If it does not work

  • If part 1 says "C5 contains 3%, expected: 5%", you are comparing with > instead of >=. Chris Walker has exactly 50,000 in sales and belongs in the top tier.
  • If part 1 is right on the visible data but fails Check, look at the boundaries. With 0 To 19999 and 20000 To 49999, sales of 19,999.50 end up in Case Else.
  • If part 2 says your message reads "Total commission 10127" instead of "Total commission: 10127", the colon is missing. The text has to match character for character, including the space after the colon.
  • If part 3 stops with "Run-time error '1004': Application-defined or object-defined error", best has no start value yet. There is no Cells(0, 2); set best = 2 before the loop.
  • If part 3 is right on the visible data but fails Check, the line that writes Bonus may be inside the loop. Then everyone who led at some point gets a bonus. Write it after Next, once the winner is known.

What you learned in level 2

Your macros now work through whole lists: For Next over row numbers, For Each over cells, Do While until a condition is met. You find the last row with End(xlUp), even when the list has gaps. Select Case sorts values into tiers, and Offset and Resize refer to cells relative to others. That covers most of the repetitive list work in an office. Level 3 adds multiple sheets, formatting and functions.

FAQ

How many points is the project worth?

Each of the three parts earns 15 points instead of the usual 10, because you combine several building blocks.

Is the project free?

No. Projects from level 2 onwards are part of Premium. The first exercise in every level 2 lesson is free, so you can still prepare without Premium.

Is there only one right answer?

No. Check looks at what your macro leaves on the sheet and in the message, not at the text of your code. For part 2, For Each is just as valid as For Next. Part 1 has to use Select Case, because that is what the task is about.

Why does the message show no thousands separator?

When you join a number to text with &, VBA converts it without separators. Formatting numbers for messages is covered in level 3.

What if I get stuck?

The hints under each task help in three steps: the idea, the structure, then almost the finished code. Step through shows you which row the macro is working on.

Level 2 complete

On to level 3. It starts with worksheets: you refer to sheets by name, add new ones and use For Each to go through every sheet in a workbook.

Learn worksheets

Write to us

Choose language

Select your preferred language