11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

616<br />

Part VI: Developing Custom Worksheet Functions<br />

If you entered the VBA code correctly, the Function procedure executes, and your name displays<br />

(in uppercase characters) in the cell.<br />

If your formula returns an error, make sure that the VBA code for the USER function is in a<br />

VBA module (and not in a module for a Sheet or ThisWorkbook object). Also, make sure<br />

that the module is in the project associated with the workbook that contains the formula.<br />

When Excel calculates your worksheet, it encounters the USER custom function, and then goes<br />

to work following the instructions. Each instruction in the function is evaluated, and the result is<br />

returned to your worksheet. You can use this function any number of times in any number of cells.<br />

You’ll find that this custom function works just like any other worksheet function. You can insert<br />

it into a formula by using the Insert Function dialog box, and it also appears in the <strong>Formula</strong><br />

AutoComplete drop-down list as you type it in a cell. In the Insert Function dialog box, custom<br />

functions appear (by default) in the User Defined category. As with any other function, you can<br />

use it in a more complex formula. For example, try this:<br />

=”Hello “&USER()<br />

Or use this formula to display the number of characters in your name:<br />

=LEN(USER())<br />

If you don’t like the fact that your name is in uppercase, edit the procedure as follows:<br />

Function USER()<br />

‘ Returns the user’s name<br />

USER = Application.UserName<br />

End Function<br />

After editing the function, reactivate Excel and press F9 to recalculate. Any cell that uses the<br />

USER function displays a different result.<br />

About Function Procedures<br />

In this section, I discuss some of the technical details that apply to Function procedures. These<br />

are general guidelines for declaring functions, naming functions, using custom functions in formulas,<br />

and using arguments in custom functions.<br />

Declaring a function<br />

The official syntax for declaring a function is as follows:

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

Saved successfully!

Ooh no, something went wrong!