18.5. Making Sure the Procedure Is Running Under Suitable Conditions

Another important consideration when creating a well-behaved procedure is to check that it's running under suitable conditions. This ideal is nearly impossible to achieve under all circumstances, but you should take some basic steps, such as the following:

  • Make sure that a file is open in a procedure that needs a file to be open — otherwise, you'll get an error every time. For example, in Excel, you might check the Count property of the Workbooks collection to make sure that at least one workbook is open:

    If Workbooks.Count = 0 Then _
        MsgBox "This procedure will not run without a" _
        & "workbook open.", vbOKOnly + vbExclamation, _
        "No Workbook Is Open"

  • Check that the procedure is referencing an appropriate item, if the procedure has definable requirements. For example, in an Excel procedure that applies intricate formatting to a chart the user has selected, make sure that the user has, in fact, actually selected a chart. Trying to manipulate another object with chart-related commands is likely to cause an error or at least unwanted side effects.

  • Make sure the file contains the element required by the procedure. (If it doesn't, an error will likely result.) Alternatively, trap the error that will result from the item's not being present.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.201.57