VBA中byref类型不匹配 "ByRef Argument Type Mismatch"错误

   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
重新运行,以上错误消失。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值