Forum Discussion
byref Type Mismatch Error when declaring multiple variables in single row
- Aug 23, 2021
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.
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.
- szilvia_vfAug 23, 2021Brass ContributorI learn something new every day. In this case I had an inappropriate declaration habit so far. Thank you! (indeed, Intger was just a typo)