XL97: Compile Error Passing ParamArray

Last reviewed: February 27, 1998
Article ID: Q157351
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

If you are passing a ParamArray to another function, you may receive the following error:

   Compile error:
   Invalid ParamArray use

CAUSE

You may receive the aforementioned error message if both of the following are true:

  • You are running your macro code in Microsoft Excel 97.

        -and-
    
  • You are passing the ParamArray to a ByRef argument in another function.

NOTE: If you run the same macro code in Microsoft Excel versions 5.0 or 7.0, you will not receive the error message.

You receive the compile error because Visual Basic for Applications in Microsoft Excel 97 does not allow you to pass a ParamArray to a ByRef argument in another function.

RESOLUTION

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.

To work around this problem, add "ByVal", without quotation marks, to the argument (receiving the ParamArray) in the function that is being called.

For example, the following macro code will return an error when you run the "Test" subroutine:

   Sub Test()

      x = myfun(1)
      MsgBox x

   End Sub

   Function myfun(y As Integer, ParamArray myarr() As Variant) As Integer

      myfun = otherfun(y, myarr)

   End Function

   Function otherfun(y As Integer, myarr As Variant) As Integer

      otherfun = y * 2

   End Function


If you change the "otherfun" function declaration to:

   Function otherfun(y As Integer, ByVal myarr As Variant) As Integer

and run the "Test" subroutine, you will get a message box with a value of 2 displayed.

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.

MORE INFORMATION

When you have an argument in the argument list of a function and you do not specify either ByVal (by value) or ByRef (by reference), the argument will be ByRef. This is the default in Visual Basic for all variables.

REFERENCES

For more information about Passing Arguments, click the Index tab in Microsoft Excel 97 Visual Basic for Applications Help, type the following text

   passing arguments

and then double-click the selected text to go to the "Passing arguments efficiently" topic.


Additional query words: XL97
Keywords : kbcode kberrmsg kbprg xlvbahowto xlvbainfo xl97vbmigrate
Version : WINDOWS:97
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: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.