user1125946
user1125946

Reputation:

Byref argument type mismatch

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

If in the start of Test2 I do one line to create my arrays:

Dim X1(5), X2(5) As Double, then it'll work. But when I use b from the function's argument list, I have to ReDim (because b is a variable, not a constant), which then causes the 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

Upvotes: 4

Views: 15235

Answers (1)

Alex K.
Alex K.

Reputation: 175766

This:

Dim X1(), X2() As Double

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

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

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

Which will mean the correct double type is passed to Test1

Upvotes: 13

Related Questions