呵呵,这是我写的代码。
我把一个函数的返回类型定义为数组,居然能正常运行。但是我无法在函数内给它赋值!!
Option ExplicitPrivate Sub Command1_Click()
Dim t()  As Long
t = test(8)
Debug.Print UBound(t, 1)
'Debug.Print t(0), t(1), t(2), t(3), t(4), t(5)
End SubPrivate Function test(ub As Long) As Long()
ReDim test(ub)
'sdfs(3) = 6
End Function

解决方案 »

  1.   

    Option ExplicitPrivate Sub Command1_Click()
        Dim t()  As Long
        t = test(8)
        Debug.Print UBound(t, 1)
        t(0) = 1
        t(1) = 2
        t(2) = 3
        t(3) = 4
        t(4) = 5
        t(5) = 6
        Debug.Print t(0), t(1), t(2), t(3), t(4), t(5)
    End SubPrivate Function test(ub As Long) As Long()
        ReDim test(ub)
        'sdfs(3) = 6
    End Function
      

  2.   

    Option ExplicitPrivate Sub Command1_Click()
        Dim t()  As Long
        t = test(8)
        Debug.Print UBound(t, 1)
        t(0) = 1
        t(1) = 2
        t(2) = 3
        t(3) = 4
        t(4) = 5
        t(5) = 6
        Debug.Print t(0), t(1), t(2), t(3), t(4), t(5)
    End SubPrivate Function test(ub As Long) As Long()
        dim a() as long
        ReDim a(ub)
        test = a
    End Function
      

  3.   

    myhgyp的回答我同意,因为刚才我也正好试过了,来结贴的.
    不过myhgyp的回答中并没有表现出这样定义有些什么用途,我补充一下吧.Option ExplicitPrivate Sub Command1_Click()
        Dim t() As Long,i as long,s as long
        i=8
        t = test(i)
        for s=0 to i
            debug.print t(s);      '显示在 test 函数中给 数组t 赋的值.
        next
    End SubPrivate Function test(ub As Long) As Long()       '声明返回类型为数组
        dim a() as long,i as long
        redim a(ub)
        for i=0 to ub
            a(i)=i^2             '给在此函数中定义的数组赋值
        next
        test=a         '这个表达的实际功能是把数组 a 的指针赋给 test 的返回值
    End Function