VBA函数似乎返回一个数组,但出现“类型不匹配:预期为数组或用户定义类型”

时间:2019-01-08 23:32:12

标签: arrays vba

我有这组VBA功能:

Sub x()
    Dim a() As Variant
    z y(a)
End Sub

Function y(a() As Variant) As Variant
    y = a
End Function

Function z(a() As Variant) As Variant
    z = a
End Function

当我运行x()时,行z y(a)会引发错误:

Type mismatch: array or user-defined type expected

我想是因为z()期望将数组作为参数,但是y()的返回值是一个变量,而不是数组。我不确定如何明确说明y()返回一个变量数组-我是否声明错误?

1 个答案:

答案 0 :(得分:1)

尽管您的代码不执行任何操作,但这将停止错误:

Sub x()
    Dim a() As Variant
    z y(a)
End Sub

Function y(a() As Variant) As Variant()
    y = a
End Function

Function z(a() As Variant) As Variant()
    z = a
End Function