Microsoft KB Archive/141784

= Clarification of Caller Property Help =

Article ID: 141784

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 5.0c
 * Microsoft Excel 5.0a for Macintosh
 * Microsoft Excel 5.0a for Macintosh

-



This article was previously published under Q141784



SUMMARY
In Microsoft Excel, the Caller Property Help topic in Microsoft Visual Basic for Applications Help contains the following statement:

If you need to write a macro which behaves differently based on whether it is called from a button or a menu item, you should specify an argument for the macro in the Assign Macro To Object... dialog box.

To help clarify this statement, follow the steps in the "Example" section in the "More Information" section of this article. These steps show you how to create a procedure that takes an argument. The steps also show you how to specify a value for an argument when you assign the procedure to an object.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Example
  Enter the following code in a module sheet: Sub MultiUse(fromMenu As Boolean) If fromMenu Then MsgBox "Called from a menu" Else MsgBox "Called from a button" End If     End Sub  Switch to a worksheet and use the Forms toolbar to create an object on your sheet. Select the object and click the right mouse button. On the shortcut menu, click Assign Macro. In the Macro Name/Reference box, type the appropriate command for the type of object.

 To assign the procedure to a menu bar, type:

'MultiUse true'

 To assign the procedure to a button, type:

'MultiUse false'

</li></ul>

NOTE: You must include the apostrophes around either statement. Note also that you can use other types of arguments; for example, you can use an integer rather than a Boolean argument. The following error message:

Formula is too complex to be assigned to object.

will be received if you try to pass an argument from within the Assign Macro dialog box using something like:

test(1)

where "test" is the name of your macro and you are trying to pass a value of 1 to the macro.</li></ol>

This method of determining how a macro was called can offer different benefits than the Application.Caller method, which can return many different data types such as a Range object, a string, an array of numbers, an array of one number and a string, or an error.

You can use information functions such as IsObject or Isarray to check the type of returned values to determine what called the macro.

When you use an argument to tell the procedure what called it (menu, tool, or button), your code is designed such that it can tell what kind of result it will get from Caller, and it can use that information to determine what else to do.

<div class="references_section">