XL97: No Automatic Type Coercion Between Variable Types

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

SYMPTOMS

When you run a Visual Basic for Applications macro in Microsoft Excel 97, you may receive the following error message:

   Compile error:
   ByRef argument type mismatch

CAUSE

This problem may occur if all of the following conditions are true:

  • Within your code, one macro is executing another macro.

        -and-
    
  • The first macro is attempting to pass a value to the second macro. The value in question is an element of an array declared as type Variant.

        -and-
    
  • The second macro is set up to accept a value from the first macro, but of a type other than Variant (Integer or Long, for example).

WORKAROUND

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.

The following workarounds assume that you have a Visual Basic module that contains two macros:

   Sub Macro1()
       Dim X As Variant
       X = ActiveWorkbook.Sheets("Sheet1").Range("A1:A5")
       Macro2 X(1, 1)          'X is an array of type Variant...
   End Sub

   Sub Macro2(Y As Integer)    '...but Macro2 expects an Integer.
       MsgBox Y
   End Sub

When run, the first macro (Macro1) initializes an array named X, populates the array, and then sends a value from the array to the second macro (Macro2). Macro2 then displays the value in a message box. If you run Macro1 without applying any of the following workarounds, you will receive the error message shown above.

Method 1

To prevent the problem from occurring, change how the second macro (Macro2) accepts values. In this case, change the second macro to

   Sub Macro2(Y As Variant)    'Y is now a Variant, not an Integer.
       MsgBox Y
   End Sub

to eliminate the problem, since an element from an array of type Variant is being sent to a variable of type Variant.

Method 2

A second way to prevent the problem from occurring is to convert the value to an Integer as you pass it to the second macro using the CInt function.

   Sub Macro1()
       Dim X As Variant
       X = ActiveWorkbook.Sheets("Sheet1").Range("A1:A5")
       Macro2 CInt(X(1, 1))  'Convert Variant X(1, 1) using CInt
   End Sub

   Sub Macro2(Y As Integer)  'Macro2 expects an Integer.
       MsgBox Y
   End Sub

Method 3

Another way to prevent the problem from occurring is to dimension a second variable in the first macro. This second variable should be of the same type as the value accepted by the second macro. The value from the array is copied into the second variable, which is then sent to the second macro. For example:

   Sub Macro1()
       'Z is the second variable. It is an Integer, just like Y.
       Dim X As Variant, Z As Integer

       X = ActiveWorkbook.Sheets("Sheet1").Range("A1:A5")
       Z = X(1, 1) 'Get the value from the array and put it in Z.
       Macro2 Z    'This works correctly.
   End Sub

Since Z is an integer, the value of Z is received by Macro2 (which expects an integer) correctly.

STATUS

This behavior is by design of Microsoft Excel 97. The behavior demonstrated by earlier versions of Microsoft Excel is actually incorrect.

MORE INFORMATION

In earlier versions of Microsoft Excel, a value can be passed from an element in an array of type Variant in one function, to a variable of another type in another function. This is called "automatic type coercion", since the value passed between functions is being forced (coerced) to switch from one type (Variant) to another type (Integer, for example).

In Microsoft Excel 97, automatic type coercion is not supported. Because of this, you may need to ensure that your macros consistently use the same types of variables when passing values from macro to macro.


Additional query words: 8.00 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.