2017-07-06 73 views
1

我只是有一個字符串數組類似的問題,現在這是工作,但沒有。我試着返回一個Integer數組作爲Integer()類型和Variant()並循環並用Cint()轉換每個元素。我得到一種類型不匹配的方式。下面是代碼:無法返回Excel中的整數數組VBA

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 
+0

我沒有得到類型不匹配。請指出代碼在哪一行失敗。 – Vegard

回答

0

首先,使函數返回一個整數數組,在函數聲明遵循返回類型與()以指示數組:

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

其次,最後一條語句改爲:

set_path_time_list = pathTimeList 

一些可選的清理項目。最後陳述後,考慮重新分配你Redim得到了存儲:

Erase pathTimeList 

最後,你應該申報ij作爲IntegerLong

希望有幫助