18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part VI<br />

Programming Excel with VBA<br />

n<br />

n<br />

n<br />

n<br />

To simplify access to worksheet functions. Custom worksheet functions stored in an add-in<br />

don’t require the workbook name qualifier. For example, if you have a custom function named<br />

MOVAVG stored in a workbook named Newfuncs.xlsm, you have to use a syntax such as the<br />

following to use this function in a different workbook:<br />

=NEWFUNC.XLSM!MOVAVG(A1:A50)<br />

But if this function is stored in an add-in file that’s open, the syntax is much simpler because you<br />

don’t need to include the file reference:<br />

=MOVAVG(A1:A50)<br />

To provide easier access. After you identify the location of your add-in, it appears in the Add-Ins<br />

dialog box and can display a friendly name and a description of what it does.<br />

To permit better control over loading. You can automatically open add-ins when Excel starts,<br />

regardless of the directory in which they’re stored.<br />

To omit prompts when unloading. When an add-in is closed, the user never sees the Save<br />

Change In prompt because changes to add-ins aren’t saved unless you specifically do so from<br />

the VB Editor window.<br />

Creating Add-Ins<br />

Technically, you can convert any workbook to an add-in. But not all workbooks benefit from this conversion.<br />

In fact, workbooks that consist only of worksheets (that is, not macros or custom dialog boxes)<br />

become unusable because add-ins are hidden.<br />

Workbooks that benefit from conversion to an add-in are those with macros. For example, you may have a<br />

workbook that consists of general-purpose macros and functions. This type of workbook makes an ideal<br />

add-in.<br />

These steps describe how to create an add-in from a workbook.<br />

1. Develop your application and make sure that everything works properly.<br />

2. Choose <strong>Office</strong> ➪ Prepare ➪ Properties to display the Properties panel above your worksheet,<br />

enter a brief descriptive title in the Title field, and then enter a longer description in<br />

the Comments field. This step isn’t required, but it makes installing and identifying the add-in<br />

easier. To close the Properties panel, click its close button (X).<br />

3. Lock the VBA project. This optional step protects the VBA code and UserForms from being<br />

viewed. You do this in the VB Editor, using the Tools ➪ projectname Properties command (where<br />

projectname corresponds to your VB project name). In the dialog box, click the Protection tab and<br />

select Lock Project For Viewing. If you like, you can specify a password to prevent others from<br />

viewing your code.<br />

4. Save the workbook as an add-in file by choosing <strong>Office</strong> ➪ Save As and selecting Excel Add-<br />

In (*.xlam) from the Save As Type drop-down list. By default, Excel saves your add-in in your<br />

AddIns directory. But you can override this location and choose any directory you like.<br />

NOTE<br />

After you save the workbook as an add-in, the original (non-add-in) workbook remains active.<br />

You should close this file to avoid having two macros with the same name.<br />

774

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!