Forum Discussion

szilvia_vf's avatar
szilvia_vf
Brass Contributor
Aug 22, 2021
Solved

byref Type Mismatch Error when declaring multiple variables in single row

Hi!   I guess I would need a bit of explanation what actually happens when I declare variables in a single row. If I do so, passing the variable to another Sub in the default ByRef method won't wor...
  • HansVogelaar's avatar
    Aug 23, 2021

    szilvia_vf 

    Unlike most other programming languages, VBA requires that you specify each variable's data type separately when you declare them on a single line. If you don't, they are declared as the default type Variant.

    So

     

        Dim A, B As Integer

     

    is equivalent to

     

        Dim A As Variant, B As Integer

     

    When you call Macro2, the argument that you pass to the ByRef argument A must be Integer (I assume that Intger was a typo). Since A is effectively declared as Variant in the line above, you get a Type Mismatch error.

    Solution:

     

        Dim A As Integer, B As Integer

     

    In this version, both A and B are declared explicitly as Integer.

Resources