Case 5: Log changes with VBA
When several people maintain the same list, sooner or later someone asks: who changed this price, and when? An event macro records every change without anyone having to remember. In the second part, an x in the Done column adds the date and name next to it automatically.
In short: Private Sub Worksheet_Change(ByVal Target As Range) in the sheet's code window runs after every entry. Intersect(Target, Range("C:D")) limits it to certain columns, Target.Address(False, False) returns "C3" without dollar signs, and Application.UserName the name from the Excel options. If the macro writes to its own sheet, put Application.EnableEvents = False before and = True after.
The situation
The office supplies price list lives on a shared drive, and three colleagues maintain prices and minimum quantities. Next to the price list there is a Log sheet with date, time, cell, new value and user, which already holds two entries from last week. From now on, every change in the Price and Min quantity columns should add a row there. Changes to the item number or description don't count.
You know the basic pattern from the events lesson. The exercise replays entries: it types values into a few cells one after another and calls Worksheet_Change each time with the changed cell as Target, just as Excel does.
Task 1: log every change
Task Several colleagues maintain the office supplies price list. So that it stays clear who changed a price or a minimum quantity and when, every change in column C or D should add a new row to the Log sheet: the date (Date) in A, the time (Time) in B, the cell without dollar signs (such as "C3") in C, the new value in D and the name from Application.UserName in E. Changes in other columns are not logged. Write the event procedure Worksheet_Change of the Prices sheet for this. The exercise enters a few values one after another.
Prices
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item no | Description | Price | Min quantity |
| 2 | 40110 | Copy paper, 500 sheets | 4.49 | 10 |
| 3 | 40125 | Lever arch file | 2.79 | 20 |
| 4 | 40133 | Ballpoint pens, blue, pack of 10 | 3.99 | 5 |
| 5 | 40141 | Sticky notes, yellow | 1.89 | 12 |
| 6 | 40158 | Black toner | 69.00 | 2 |
| 7 | 40162 | Envelopes, pack of 100 | 5.49 | 4 |
Log
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Date | Time | Cell | New value | User |
| 2 | 12 Mar 2026 | 14:05 | C5 | 1.89 | J. Patel |
| 3 | 13 Mar 2026 | 08:47 | D3 | 20 | S. Brooks |
| 4 | |||||
| 5 |
You find the next free row in the log as in the last row lesson, just with the sheet in front. Date and time go into two columns so the log is easy to filter by day later. Because the macro only writes to the Log sheet, it does not fire another event on the price list.
| Expression | Result for cell C3 |
|---|---|
Target.Address | $C$3 |
Target.Address(False, False) | C3 |
Target.Row, Target.Column | 3, 3 |
In the exercise, Application.UserName always returns "Excel user". In real Excel it is the name under File > Options > General, which anyone can change there. That is fine for an office log, but it is no proof against tampering.
Task 2: a "done" stamp with an x
A second list, the same idea: in the list of requests, staff put an x in column D when a request has been dealt with. The date and name should appear next to it by themselves and disappear again if the x is deleted.
Task In the list of requests, staff put an "x" in column D when a request has been dealt with. Today's date (Date) should then appear in E and the name from Application.UserName in F automatically. If the "x" is removed again or replaced with something else, E and F are cleared. Upper or lower case x makes no difference. Changes in other columns do nothing. Write the event procedure Worksheet_Change and turn events off with Application.EnableEvents while the macro writes to the sheet itself. The exercise enters a few values one after another and also deletes an x once.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | No | Subject | Received | Done | Done on | Done by |
| 2 | A-031 | Resident parking permit, Garden Street | 2 Mar 2026 | x | 12 Mar 2026 | J. Patel |
| 3 | A-032 | Wedding date request | 4 Mar 2026 | |||
| 4 | A-033 | Business registration query | 5 Mar 2026 | |||
| 5 | A-034 | Bulky waste collection | 9 Mar 2026 | |||
| 6 | A-035 | Copy of planning application | 10 Mar 2026 | |||
| 7 | A-036 | Street lighting complaint | 12 Mar 2026 |
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.
This time the macro writes to its own sheet. To stop that from firing Worksheet_Change again, it turns events off briefly. Here it would even be harmless without that, because E and F are not in column D. It is still the usual, safe form, because as soon as someone extends the macro, an endless loop is quickly created otherwise. The check Target.Count > 1 catches someone pasting or deleting several cells at once; Target.Value would then be an array, and comparing it with "X" a run-time error.
Common mistakes
Address with dollar signs
Target.Address without arguments returns an absolute address. The exercise reports: "Log!C4 contains "$C$3", expected: "C3". 1 more cell is different as well."
Only a lower-case x recognised
Comparing Target.Value = "x" misses the capital X. The exercise reports: "E5 is empty, expected: 16 Mar 2026." Row 5 got a capital X. UCase or LCase before the comparison treats both the same.
Events stay off
If the macro stops with an error between EnableEvents = False and = True, events stay switched off until you turn them back on in the Immediate window or restart Excel. Longer event macros therefore need error handling that always reaches the line that turns them back on.
Code in the wrong module
If Worksheet_Change sits in a normal module instead of the sheet's code window, nothing happens on entry, and there is no message. In the exercises the code always sits in the code window of the first sheet.
In real Excel
Right-click the sheet tab, choose View Code, pick "Worksheet" on the top left and "Change" on the right, add your code and save as .xlsm. The log only works when macros are enabled in the file; anyone who opens it without macros changes things unlogged. More in the enable macros lesson. If a log is mandatory, for example for prices that need approval, it belongs in a system with user sign-in, not in an Excel file.
Check your result
- Task 1: the log has two new rows, for C3 with 2.95 and for D2 with 15. The change to the item number in A5 does not appear.
- Task 2: rows 3 and 5 show the date 16 Mar 2026 and "Excel user", and row 2 is empty again because its x was deleted.
Back to case 4 or to the course overview.