VBA中子函数调用时出现如下错误:"ByRef Argument Type Mismatch",(参数类型不匹配)代码如下:
Function MainFunc()
Dim a, b, c As Integer
a = 111
b = 222
Call AddFunc(a, b, c)
MsgBox c
End Function
Sub AddFunc(a As Integer, b As Integer, Sum As Integer)
Sum = a + b
End Sub
原来是变量定义在捣鬼,在C,或者C++中,多个同类型的变量定义可以像下面这样:
int a, b, c ;
而如果在VB中,如下定义的话:
Dim a, b, c As Integer
却只表示变量c 是Interger类型,而变量a,b都是变体型,也就是任意类型,所以当函数传值调用的时候,会报上述错误!
修改后的代码如下:
Function MainFunc()
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = 111
b = 222
Call AddFunc(a, b, c)
MsgBox c
End Function
Sub AddFunc(a As Integer, b As Integer, Sum As Integer)
Sum = a + b
End Sub
重新运行,以上错误消失。