无法在Excel VBA中返回整数数组

时间:2017-07-06 17:10:33

标签: vba excel-vba excel

我只是遇到类似String数组的问题,现在它正在工作,但这不是。我尝试将Integer数组作为Integer()类型和Variant()返回并循环并使用Cint()转换每个元素。我得到一种类型不匹配的方式。这是代码:

Dim pathTimeList() As Integer
ReDim pathTimeList(0 To stepCount)
pathTimeList = set_path_time_list(stepCount)

这是功能代码:

Private Function set_path_time_list(ByVal stepCount As Integer) As Integer


    Dim pathTimeList() As Integer
    ReDim pathTimeList(0 To stepCount - 1)

    Dim loopIndex As Integer
    loopIndex = 0

    Dim firstRow As Integer
    Dim lastRow As Integer
    Dim firstColumn As Integer
    Dim lastColumn As Integer

    firstRow = 3
    lastRow = 27
    firstColumn = 2
    lastColumn = 2

    For i = firstRow To lastRow

        For j = firstColumn To lastColumn

            pathTimeList(loopIndex) = Cells(i, j).Value

        Next j

        loopIndex = loopIndex + 1

    Next i

    set_path_time = pathTimeList

End Function

1 个答案:

答案 0 :(得分:0)

首先,要使函数返回一个整数数组,请在函数声明中使用返回类型()来表示数组:

Private Function set_path_time_list(ByVal stepCount As Integer) As Integer()

其次,将最后一个语句改为:

set_path_time_list = pathTimeList

一些可选的清理用品。在最后一个语句之后,考虑使用Redim解除分配存储:

Erase pathTimeList

最后,您应将ij声明为IntegerLong

希望有所帮助