声明函数返回类型导致失败

时间:2019-04-08 01:24:14

标签: excel vba

我想将选择的对角线输出到垂直连续的单元格区域。使用模型是,您选择要输出到的单元格范围,然后开始键入= diagonals()

这有效:

Option Explicit
Option Base 1

Function Diagonals(rng As Range)

    Dim i As Integer
    Dim size As Integer
    size = IIf(rng.Rows.Count > rng.Columns.Count, rng.Columns.Count, rng.Rows.Count)

    Dim A()
    ReDim A(size, 1)

    For i = 1 To size
        A(i, 1) = rng.Cells(i, i)
    Next i

    Diagonals = A

End Function

但是,事实并非如此,并且没有错误消息。有见识吗?

Option Explicit
Option Base 1

Function Diagonals(rng As Range) As Range

    Dim i As Integer
    Dim size As Integer
    size = IIf(rng.Rows.Count > rng.Columns.Count, rng.Columns.Count, rng.Rows.Count)

    Dim A() As Range  ' declaring type here , or for Function causes fail
    ReDim A(size, 1)

    For i = 1 To size
        A(i, 1) = rng.Cells(i, i)
    Next i

    Diagonals = A

End Function

0 个答案:

没有答案