Byref Argument Type Mismatch

Type =Test2(5) in Excel after you put the code in the module. Why does this give me a Byref argument type mismatch error?

If at the beginning of Test2 I make one line to create my arrays:

Dim X1(5), X2(5) As Double , then it will work. But when I use b from the function argument list, I have to ReDim (because b is a variable, not a constant), which then causes an error.

  Function Test1(a As Double) Test1 = a * 2 End Function Function Test2(b As Integer) Dim X1(), X2() As Double ReDim X1(b), X2(b) As Double Dim i As Integer For i = 0 To b X1(i) = i X2(i) = Test1(X1(i)) Next i Test2 = X2(1) End Function 
+4
source share
1 answer

It:

 Dim X1(), X2() As Double 

declares only X2() as double, X1() will store type b (integer) instead of b , converted to double (and thus prevent the transfer of As Double ).

To make them double, you must repeat the type declaration;

 Dim X1() As Double, X2() As Double ReDim X1(b), X2(b) 

This means that the correct double type is passed to Test1

+12
source

Source: https://habr.com/ru/post/1433777/


All Articles