Professional Excel Development [Electronic resources] : The Definitive Guide to Developing Applications Using Microsoft® Excel and VBA®

Stephen Bullen, Rob Bovey, John Green

نسخه متنی -صفحه : 225/ 156
نمايش فراداده

Debugging the Worksheet Functions

In the course of writing custom worksheet functions, you will need to do some debugging in order to fix errors and prove that your function is operating as intended. This is very easy to do. Because we have already specified Excel.exe as our debug executable, all that's required is to compile your XLL using the Build > Build Solution menu, put a break point somewhere in your function and press F5 to start debugging. Visual Studio will start Excel for you. The first time you do this you'll be prompted with the warning dialog shown in Figure 19-9.

Figure 19-9. The First-Time Excel.exe Debug Warning Dialog

This is just telling you that you won't be able to debug Excel itself because there's no debugging information available. Because that's not what we're trying to do here, you can safely check the Do not prompt in the future check box and click OK to continue.

When Excel is open, you will have to make sure your XLL is also open inside Excel. This does not happen automatically. You can open your XLL in one of two ways. Either use the File > Open menu to open the XLL directly, or add the XLL to the list of add-ins that Excel loads automatically on startup. The second method is the preferred method for use over multiple debugging sessions.

To do this, choose Tools > Add-ins from the Excel menu to display the Add-ins dialog. Click the Browse button on the Add-ins dialog and use the Browse window to point Excel at your XLL. If you have both debug and release versions of your XLL, be sure to point Excel at the debug version. Click OK twice and your XLL will be loaded.

Now all you need to do is enter your custom worksheet function into a worksheet cell. As soon as code execution reaches your break point it will stop and you can begin debugging your code.