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

Last reviewed: September 2, 1997
Article ID: Q108814
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0

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 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.

  1. 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", without quotes.

  • Add two new buttons to the dialog sheet.

  • Change the text of one button to "PassArg", without quotes, and assign the ArgButton_Click macro to this button.

  • Change the text of the other button to "Done", without quotes, and assign the DoneButton_Click macro to this button.

  • Run the MainMacro subroutine.

  • The dialog box will appear – click the "PassArg" button.

    The dialog box will hide itself and a message box will appear with "Here is the Argument" displayed.

    1. 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.

    1. Click the "Done" button to dismiss the dialog box and stop the MainMacro subroutine.

    REFERENCES

    For more information about the Show Method, choose the Search button in Help and type:

       dialog boxes: displaying for user input
    

  • Additional query words: 5.00 7.00
    Keywords : kbprg PgmOthr
    Version : 5.00 5.00c 7.00
    Platform : WINDOWS


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: September 2, 1997
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.