Microsoft KB Archive/118881

= Microsoft Knowledge Base =

XL5: Unexpected Results Calling Sub Procedure with Arguments
Last reviewed: May 29, 1997

Article ID: Q118881

5.00c  | 5.00 5.00a WINDOWS | MACINTOSH kbprg kbcode

The information in this article applies to:


 * Microsoft Excel for Windows, version 5.0c
 * Microsoft Excel for the Macintosh, versions 5.0, 5.0a

SYMPTOMS
In Microsoft Excel, when you use the Run method of the Application object to call a Visual Basic, Applications Edition, sub procedure that has arguments from another procedure, the procedure you call may not run as expected, and any of the following symptoms may occur:


 * The procedure you call doesn't run at all
 * The procedure you call stops running before all the commands in the procedure have been run
 * Only some of the commands are run in the procedure that you call

CAUSE
This behavior occurs when you call a procedure that has an argument, and you call the procedure using the following syntax:

Application.Run("Test(1)") When you call a procedure that uses an argument using this syntax, certain commands do not run. Any command that takes some action on the active workbook, such as inserting a sheet, opening another window, or opening another workbook, does not run. Additionally, if a command in the procedure that you call causes an error in the macro, you do not receive an error message, but the macro stops running.

Note that this behavior is different from the behavior in Microsoft Excel for Windows, version 5.0. When you use the syntax above to call a sub procedure in Microsoft Excel version 5.0, the procedure runs as expected.

WORKAROUND
To work around this behavior, when you call a procedure that has an argument using the Run method of the Application object, enclose only the procedure name in quotation marks, followed by the arguments as in the following example:

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Sub Run_Test ' Run Test procedure Application.Run "Test", 1 End sub

Sub Test(num as Integer) ThisWorkbook.Modules.Add MsgBox num End Sub Note that you can also use the following syntax to run the sub procedure that has an argument:

Test(1)

STATUS
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.