Cat On A Spreadsheet
When you’re working on small macros, a simple MsgBox error handler might be enough. But as your projects grow in size and complexity, error handling needs to evolve too. For large VBA projects, it’s not just about stopping errors gracefully — it’s about capturing useful debugging information automatically, so you can fix issues quickly without relying on users to describe what went wrong.
In this post, we’ll walk through building an error logging framework that writes useful details to a central location, such as a dedicated worksheet or a text file.
Imagine you’ve distributed your VBA project to 50 users. One day, a user runs into an issue. Instead of getting a vague “Runtime Error 91,” your error logger could provide:
The exact procedure where the error occurred
The error number and description
The time and date
Optionally, the username and machine name
This transforms debugging from guesswork into a straightforward process.
At its simplest, you can wrap procedures with an error handler:
This works, but it only shows an error message to the user. What we really want is a reusable system that logs these details automatically.
Instead of writing error-handling code in every macro, create a centralized subroutine that records error details:
This routine assumes you have a worksheet named ErrorLog with columns for:
Timestamp
Procedure Name
Error Number
Error Description
User Name
Machine Name
Now, instead of writing custom error handling in every macro, you can reuse the logger:
If the file doesn’t exist, the error gets recorded in your ErrorLog sheet automatically, complete with time, user, and details.
For larger teams, it can make sense to log to a text file instead of cluttering the workbook. Here’s a version that appends error logs to a .txt file:
This version creates (or appends to) a file called ErrorLog.txt in the workbook’s folder.
You can extend this error-logging approach by adding:
Line numbers with the Erl function to pinpoint where the error occurred
Stack tracing (manually passing procedure names down the call chain)
Email notifications when critical errors occur
Configurable logging levels (e.g., only log critical vs log everything)
By automating error logging, you shift from reactive debugging to proactive error management. Instead of frustrated users reporting vague issues, you’ll have a clear, timestamped history of what went wrong, where, and for whom. This framework also helps you spot patterns — if many users encounter the same issue, you’ll know exactly where to focus your fixes.
Cat On A Spreadsheet