vba Byref 参数类型不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12386935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:42:32  来源:igfitidea点击:

Byref argument type mismatch

vba

提问by Alex K.

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

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

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

如果在开始时Test2我做一行来创建我的数组:

Dim X1(5), X2(5) As Double, then it'll work. But when I use bfrom the function's argument list, I have to ReDim(because bis a variable, not a constant), which then causes the error.

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

回答by Alex K.

This:

这个:

Dim X1(), X2() As Double

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

仅声明X2()为双精度,X1()将存储b(整数)的类型而不是b转换为双精度(因此防止传递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

这将意味着正确的双精度类型被传递给 Test1