ID: Q141784
The information in this article applies to:
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.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/
1. 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
2. Switch to a worksheet and use the Forms toolbar to create an object on
your sheet.
3. Select the object and click the right mouse button.
4. On the shortcut menu, click Assign Macro.
5. 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'
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.
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.
"Microsoft Excel 5 Visual Basic for Applications Reference," Microsoft Press, pages 88-89
For more information about the Caller property, choose the Search button in Help and type:
Caller Property
Additional query words: 5.00 help file
Keywords : kbcode PgmOthr
Version : WINDOWS: 5.0, 5.0c; MACINTOSH: 5.0, 5.0a
Platform : MACINTOSH WINDOWS
Issue type : kbinfo
Last Reviewed: May 17, 1999