Welcome back. In our last lesson, you learned how to create User-Defined Functions (UDFs), which allow you to extend Excel's formula library with your own custom calculations. UDFs are a powerful way to run VBA code, but they are passive—they only execute when a user enters the formula into a cell and Excel recalculates.
Today, we will explore a more dynamic and automated way to run your code: VBA Events. You'll learn how to write macros that trigger automatically in response to specific user actions, such as changing a cell's value, opening a workbook, or adding a new sheet. This is a fundamental concept in application development and a crucial step toward your goal of building professional, interactive tools for clients.
1. Understanding Event-Driven Programming
So far, you've run macros either by manually clicking a "Run" button or by using a UDF. Event-driven programming flips this on its head. Instead of you telling the macro when to run, the macro listens for specific actions—or "events"—and runs itself when one occurs.
This allows you to create spreadsheets that feel more like applications. Imagine a timesheet that automatically timestamps a row when you type "IN", or a financial model that resets to a "start" page every time it's closed. These are all powered by events.
There are several categories of events, but we will focus on the two most common and useful types:
- Worksheet Events: Actions that happen on a specific worksheet (e.g., changing a selection, editing a cell).
- Workbook Events: Actions that affect the entire workbook file (e.g., opening, saving, closing).
A critical concept to grasp from the start is that event code cannot be placed in a standard module like the subroutines and functions you've written so far.
Workbook & Worksheet Events in Excel VBA
This article from My Online Training Hub clearly explains the most important rule about events.
Please read the section titled Location of Event Code. This is the number one reason beginners struggle with events, so it's essential to understand this rule first.
As the article states, worksheet event code must reside in that specific sheet's code module, and workbook event code must be in the ThisWorkbook module. You can access these modules from the Project Explorer window in the VBE.
2. Worksheet Events: Responding to Cell Changes
The most frequently used worksheet event is Worksheet_Change. It fires every time a cell's value is modified, either by the user or by another macro. This event procedure provides a very useful argument called Target, which is a Range object representing the cell or cells that were just changed.
Let's dive into a detailed, practical video that shows how to harness this event. It also covers three essential best practices that separate amateur code from professional code.
Excel VBA: The Magic of the Worksheet Change Event
The video "Excel VBA: The Magic of the Worksheet Change Event" by Excel Macro Mastery is a perfect guide to this topic.
Please watch the following segments carefully: Introduction (basic example): See a simple demonstration of what the Change event does. Implementation (accessing the event): Learn how to access the Worksheet_Change procedure in the VBE and understand the Target parameter. Best Practice 1: Limiting Scope (using Intersect): This is vital. You rarely want your code to run when any cell changes. This section shows how to use Application.Intersect to check if the change occurred within a specific range you care about. It also shows how to check Target.Cells.Count to ensure only one cell was modified. Best Practice 2: Preventing Infinite Loops (enabling/disabling events): This is the most critical part. If your Change event code itself changes a cell, it will trigger the event again, creating an infinite loop that can crash Excel. You'll learn to use Application.EnableEvents = False before making changes and Application.EnableEvents = True afterward to prevent this.
Hands-On Practice: Automated Task Tracker
Let's apply what you've learned. Imagine you are building a simple task list for a client. They want column B to be automatically timestamped with the completion date whenever the status in column A is changed to "Done".
- Open a new workbook. On
Sheet1, label cellA1as "Status" andB1as "Completion Date". - Open the VBE (
Alt+F11). In the Project Explorer, find your workbook and double-clickSheet1to open its code module. - Use the dropdowns at the top of the code window. Select
Worksheetfrom the left dropdown, andChangefrom the right dropdown. VBA will automatically create thePrivate Sub Worksheet_Change(ByVal Target As Range)procedure for you.

- Now, write the code to perform the automated timestamping, incorporating the best practices from the video.
Private Sub Worksheet_Change(ByVal Target As Range)
' Best Practice: Only run if a single cell in column A is changed.
' We check that the change is not in row 1 to avoid the header.
If Target.Cells.Count > 1 Or Intersect(Target, Me.Columns(1)) Is Nothing Or Target.Row = 1 Then
Exit Sub
End If
' Check if the new value is "Done" (case-insensitive)
If LCase(Target.Value) = "done" Then
' Best Practice: Disable events to prevent infinite loops
Application.EnableEvents = False
' Add the timestamp in the adjacent cell in column B
Target.Offset(0, 1).Value = Now()
Target.Offset(0, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
' Best Practice: Re-enable events
Application.EnableEvents = True
End If
End Sub
Go back to Sheet1 and try it out. Type "Done" into any cell in column A (e.g., A2), and watch the current date and time appear instantly in column B.
3. Workbook Events: Automating at the File Level
While worksheet events are tied to a single sheet, workbook events respond to actions affecting the entire file. This is perfect for automating setup and cleanup tasks, making your tools more robust and user-friendly.
Common workbook events include:
Workbook_Open: Runs when the workbook is opened.Workbook_BeforeClose: Runs just before the workbook closes.Workbook_BeforeSave: Runs just before a save operation.Workbook_NewSheet: Runs when a new worksheet is added.

The following video demonstrates how to combine these events to build a professional-feeling application with a "login" screen. This directly relates to your goal of building sophisticated freelance tools.
VBA Secrets Revealed: Build Pro Apps with Event Triggers
The video "VBA Secrets Revealed: Build Pro Apps with Event Triggers" from Excel For Freelancers shows excellent real-world applications.
Watch the section from workbook events in practice. The instructor demonstrates a powerful use case: Using the Workbook_BeforeClose event to automatically hide all worksheets except for a "Start Screen". Using the Workbook_Open event to show a login form (we'll cover UserForms later, but the principle is the same). When the user "logs in," all the sheets are unhidden. This approach ensures that every time a client opens your tool, they start from a clean, designated entry point, and the workbook is "locked down" when they close it.
This technique of controlling the user's view on open and close is a hallmark of a well-designed Excel application. It provides a polished and controlled user experience, which is exactly what clients look for in a custom solution.
Conclusion
You have now added a major component of application development to your skillset. By mastering events, you can create VBA code that runs intelligently and automatically, making your Excel solutions more powerful, interactive, and professional.
Key Takeaways:
- Event-driven programming allows macros to run automatically in response to user actions.
- Event code must be placed in the correct object module: sheet code goes in the sheet module, and workbook code goes in the
ThisWorkbookmodule. - The
Worksheet_Changeevent is extremely versatile for automating actions when data is entered or modified. - It is critical to use
Application.EnableEvents = False/Truewithin aChangeevent that modifies other cells to prevent infinite loops. - Workbook events like
Workbook_OpenandWorkbook_BeforeCloseare perfect for managing the state of your application, such as showing an introduction screen or performing cleanup tasks.
In our next lesson, we will begin exploring UserForms. Your understanding of events is the perfect preparation, as UserForms are entirely event-driven. Every button click, dropdown selection, and checkbox tick is an event that you will learn to capture and respond to with your VBA code.