Microsoft KB Archive/108814

= Can't Use Arguments to Call Sub When Another Procedure Running =

Article ID: 108814

Article Last Modified on 8/15/2005

-

APPLIES TO


 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0c

-



This article was previously published under Q108814



SYMPTOMS
If you run a Microsoft Visual Basic Programming System, Applications Edition, sub procedure that has arguments when another Visual Basic procedure is running, you may receive the following error message:

Can't perform requested operation



CAUSE
You cannot call a Visual Basic sub procedure that has arguments if a Visual Basic procedure is running at the time. Take for example a custom dialog box with a control button assigned to a Visual Basic procedure with an argument. If you run the dialog box using the "Run Dialog" toolbar button on the Forms toolbar, you can choose the button and the procedure assigned to that button is run. However, if you show the dialog box from another Visual Basic procedure, and you click the button assigned to the procedure that has arguments, you receive the error message above, because the first Visual Basic procedure is still running. When you run the dialog box from the Visual Basic procedure, the calling procedure is suspended while the dialog box is displayed.



WORKAROUND
Although you cannot call a Visual Basic sub procedure that has arguments if a Visual Basic procedure is running, you can do either of the following:


 * Call a Visual Basic subroutine that does not have arguments while a Visual Basic procedure is running -or-


 * Model your macro code after 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 professionals 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.

  Enter the following in a new module sheet: Public DoneFlag As Integer, ArgFlag As Integer

Sub MainMacro ArgFlag = 0                          'initialize ArgFlag DoneFlag = 0                         'initialize DoneFlag DialogSheets("Dialog1").Show         'Show it initially

'While the DoneFlag does not equal 1 (which will only occur if the  'DoneButton is clicked), continue to loop through the Subroutine.

Do     If ArgFlag = 1 Then                 'if the ArgFlag is set, then HasArg ("Here is the Argument") 'Arg Sheet1 and ArgFlag = 0                     'reset the ArgFlag DialogSheets("Dialog1").Show    'Reshow it only after End If                             'having called the procedure Loop Until DoneFlag = 1               'that hid it End Sub

Sub DoneButton_Click DoneFlag = 1                         'set the DoneFlag DialogSheets("Dialog1").Hide         'hide the dialog box End Sub

Sub ArgButton_Click DoneFlag = 0                         'ensure DoneFlag set to 0 ArgFlag = 1                        'set the ArgFlag DialogSheets("Dialog1").Hide         'hide the dialog box End Sub

Sub HasArg(Arg As String)  'do not assign this macro to any button MsgBox Arg End Sub  Insert a new dialog sheet and make sure it is named Dialog1 . Add two new buttons to the dialog sheet. Change the text of one button to PassArg, and assign the ArgButton_Click macro to this button. Change the text of the other button to Done, and assign the DoneButton_Click macro to this button. Run the MainMacro subroutine.</li> The dialog box will appear [ASCII 150] click the "PassArg" button.

The dialog box will hide itself and a message box will appear with "Here is the Argument" displayed.</li> Click OK in the message box.

The dialog box reappears. This will repeat itself as long as you click the "PassArg" button on the dialog box.</li> Click the "Done" button to dismiss the dialog box and stop the MainMacro subroutine.</li></ol>

<div class="references_section">