Byref参数类型不匹配

时间:2012-09-12 11:18:04

标签: vba

将代码放入模块后,将=Test2(5)键入Excel。为什么这会给我一个Byref argument type mismatch错误?

如果在Test2的开头,我会创建一行来创建我的数组:

Dim X1(5), X2(5) As Double,然后就行了。但是当我从函数的参数列表中使用b时,我必须ReDim(因为b是变量,而不是常量),然后导致错误。

 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

1 个答案:

答案 0 :(得分:12)

此:

Dim X1(), X2() As Double

仅将X2()声明为double,X1()会将b(整数)的类型存储为b转换为double(因此阻止传递{{1} }})。

要使它们都加倍,你必须重复类型声明;

As Double

这意味着将正确的double类型传递给Dim X1() As Double, X2() As Double ReDim X1(b), X2(b)