Troubleshooting Errors in VBA Macros

Some people are VBA gurus whilst others prefer to stay in regular Microsoft Office land completely oblivious to this powerful language that lurks beneath the covers. As for myself, I am just a bit of a hack that can cobble bits of useful looking code together to suit my immediate purposes.

Of course, my hacky approach often leads to bugs that require ironing out with some easier to identify than others. Knowing how to troubleshoot can save you loads of time particularly when you can rule out assumptions once you have proven something to be fact.

One such issue can be encountered with variables. Creating them is easy enough as well as assigning them a value (although you can make errors here) but often using the variables as dynamic inputs to things such as filenames and folder structures can cause some troubles if you aren’t careful. Sometimes it can help just to see what your variables actually contain before you get too far down the garden path.

Fortunately, you can achieve this rather easily by using the MsgBox function in VBA. As might extrapolate, MsgBox stands for “message box” and allows you to create a popup message box on the screen containing text. You can insert your variable as the text to be displayed in the box to see what value it holds as follows:

MsgBox(variableNameGoesHere)

Of course, you can expand it to include multiple variables and execute this function numerous times during a given macro to see how variabiles are updated and manipulated.

Leave a Reply

Your email address will not be published.