Microsoft Office Tutorials and References
In Depth Information
Simple Functions
Function SHEETNAME2() As String
‘ Returns the sheet name of the cell that
‘ contains the function
SHEETNAME2 = Application.Caller.Parent.Name
End Function
In this function, the Caller property of the Application object returns a Range object that
corresponds to the cell that contains the function. For example, suppose that you have the
following formula in cell A1:
=SHEETNAME2()
When the SHEETNAME2 function is executed, the Application.Caller property returns a
Range object corresponding to the cell that contains the function. The Parent property returns
the Worksheet object, and the Name property returns the name of the worksheet.
Returning a workbook name
The next function, WORKBOOKNAME , returns the name of the workbook. Notice that it uses the
Parent property twice. The first Parent property returns a Worksheet object, the second
Parent property returns a Workbook object, and the Name property returns the name of the
workbook.
Function WORKBOOKNAME() As String
‘ Returns the workbook name of the cell
‘ that contains the function
WORKBOOKNAME = Application.Caller.Parent.Parent.Name
End Function
Returning the application’s name
The following function, although not very useful, carries this discussion of object parents to the
next logical level by accessing the Parent property three times. This function returns the name
of the Application object, which is always the string Microsoft Excel.
Function APPNAME() As String
‘ Returns the application name of the cell
‘ that contains the function
APPNAME = Application.Caller.Parent.Parent.Parent.Name
End Function
 
Search JabSto ::




Custom Search