Ad space (banner)
Lesson 19 / 20

Recording macros and event procedures

This lesson covers Excel's macro recorder and the idea of an event procedure, which runs in response to a particular action.

Excel can record what you actually do and turn it into VBA code automatically. An event procedure, such as Worksheet_Change, is a special procedure that runs automatically at a particular moment — when a cell is changed, for instance.

Since actual Excel operations are not possible in the browser, this lesson only walks through the structure of the code. Writing an event procedure with the prescribed name and arguments, as in Private Sub Worksheet_Change(ByVal Target As Range), makes it run automatically when the matching action occurs.

A common early stumble is being satisfied with the recorder's output and carrying on using it without ever understanding what it does. Reading and understanding recorded code is the quickest way to get good at VBA.

In professional work, a common approach is to sketch out the rough flow with the recorder, then add loops and branching by hand to turn it into a macro general enough to reuse.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)