PDA

View Full Version : Application.Caller



Transformer
05-01-2013, 12:32 PM
Application.Caller returns Range if VBA code is called from a Cell/Range.
e.g. If a UDF(User Defined Function) is called from a Cell/Range and one wants to get the address of the Cell/Range ,where it is called from.
Then it can be achieved using the following code and as in this case it returns Range so you can also set it to some object also.


Function Test()

Dim objCaller As Object '/Range

Set objCaller = Application.Caller
Debug.Print objCaller.Address '/Application.Caller.address
Test = objCaller.Address

End Function

Application.Caller returns String if the caller is some control (e.g. DropDown,ListBox and CommandButton etc.) or Shape.

e.g. if the following function is called from some shape/control then it will print and return shape/control's name.


Function ControlCaller()

Debug.Print Application.Caller
ControlCaller= Application.Caller

End Function

Application.Caller returns Error if the caller is a VBA procedure or function.
e.g if you call the above given function ControlCaller in some procedure or function then it will return an error.


Sub CallIt()

Call ControlCaller()

End Sub

Note: Application.Caller does not work in Immediate Window