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.

Chapter 25: VBA Custom Function Examples 701<br />

WksNum = 1<br />

For Each wks In Application.Caller.Parent.Parent.Worksheets<br />

If Application.Caller.Parent.Name = wks.Name Then<br />

SHEETOFFSET = Worksheets(WksNum + Offset)_<br />

.Range(cell(1).Address).Value<br />

Exit Function<br />

Else<br />

WksNum = WksNum + 1<br />

End If<br />

Next wks<br />

End Function<br />

The SHEETOFFSET function accepts two arguments:<br />

offset: The sheet offset, which can be positive, negative, or 0.<br />

cell: (Optional) A single-cell reference. If this argument is omitted, the function uses the<br />

same cell reference as the cell that contains the formula.<br />

For more information about optional arguments, see the section, “Using optional arguments,”<br />

later in this chapter.<br />

The following formula returns the value in cell A1 of the sheet before the sheet that contains the<br />

formula:<br />

=SHEETOFFSET(–1,A1)<br />

The following formula returns the value in cell A1 of the sheet after the sheet that contains the<br />

formula:<br />

=SHEETOFFSET(1,A1)<br />

Advanced Function Techniques<br />

In this section, I explore some even more advanced functions. The examples in this section demonstrate<br />

some special techniques that you can use with your custom functions.<br />

Returning an error value<br />

In some cases, you may want your custom function to return a particular error value. Consider<br />

the simple REVERSETEXT function, which I presented earlier in this chapter:

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

Saved successfully!

Ooh no, something went wrong!