Thursday 25 February 2010

Excel VBA Run-time Errors and Run-time Error Handlers

We've seen compile errors in Excel VBA. How about some run-time errors?

Run-time error '1004'
AutoFit method of Range class failed

If you do Selection.AutoFit, instead of Selection.Columns.AutoFit. In Dot Net, this kind of cr- gets caught at compile-time. As an interesting side, you can explicitly set the column width in terms of number of characters.

You can develop your own run-time error handler and reference it using "On Error Goto (Insert Name of RTEH here)" but please note it will not be re-entrant.

No comments: