11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

672<br />

Part VI: Developing Custom Worksheet Functions<br />

Function SHEETNAME2() As String<br />

‘ Returns the sheet name of the cell that<br />

‘ contains the function<br />

SHEETNAME2 = Application.Caller.Parent.Name<br />

End Function<br />

In this function, the Caller property of the Application object returns a Range object that<br />

corresponds to the cell that contains the function. For example, suppose that you have the following<br />

formula in cell A1:<br />

=SHEETNAME2()<br />

When the SHEETNAME2 function is executed, the Application.Caller property returns a<br />

Range object corresponding to the cell that contains the function. The Parent property returns<br />

the Worksheet object, and the Name property returns the name of the worksheet.<br />

Returning a workbook name<br />

The next function, WORKBOOKNAME, returns the name of the workbook. Notice that it uses the<br />

Parent property twice. The first Parent property returns a Worksheet object, the second<br />

Parent property returns a Workbook object, and the Name property returns the name of the<br />

workbook.<br />

Function WORKBOOKNAME() As String<br />

‘ Returns the workbook name of the cell<br />

‘ that contains the function<br />

WORKBOOKNAME = Application.Caller.Parent.Parent.Name<br />

End Function<br />

Returning the application’s name<br />

The following function, although not very useful, carries this discussion of object parents to the<br />

next logical level by accessing the Parent property three times. This function returns the name<br />

of the Application object, which is always the string Microsoft Excel.<br />

Function APPNAME() As String<br />

‘ Returns the application name of the cell<br />

‘ that contains the function<br />

APPNAME = Application.Caller.Parent.Parent.Parent.Name<br />

End Function

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

Saved successfully!

Ooh no, something went wrong!