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

ABCD
1Item noDescriptionPriceMin quantity
240110Copy paper, 500 sheets4.4910
340125Lever arch file2.7920
440133Ballpoint pens, blue, pack of 103.995
540141Sticky notes, yellow1.8912
640158Black toner69.002
740162Envelopes, pack of 1005.494

Log

ABCDE
1DateTimeCellNew valueUser
212 Mar 202614:05C51.89J. Patel
313 Mar 202608:47D320S. 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.

ExpressionResult for cell C3
Target.Address$C$3
Target.Address(False, False)C3
Target.Row, Target.Column3, 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.

ABCDEF
1NoSubjectReceivedDoneDone onDone by
2A-031Resident parking permit, Garden Street2 Mar 2026x12 Mar 2026J. Patel
3A-032Wedding date request4 Mar 2026
4A-033Business registration query5 Mar 2026
5A-034Bulky waste collection9 Mar 2026
6A-035Copy of planning application10 Mar 2026
7A-036Street lighting complaint12 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.

Next: case 6, weekly summary

At the push of a button, the team's task list turns into a summary with counts per status and per owner, plus a print-ready week sheet.

Go to case 6

Back to case 4 or to the course overview.

Write to us

Choose language

Select your preferred language