2016-03-30 194 views
0

我在VBA中有兩個函數。 Function1返回一維數組。然後我有一個多維數組function2。我想將Function1中的數組複製到從索引1開始的多維數組的列中。複製一維數組到多維數組 - VBA

arr2(0,0) = "Something" 
arr2(0,1) = ("Something",arr1(0)) 
arr2(0,2) = ("Something",arr1(1)) 

這就是我所擁有的。 arr1是GetRecData,arr2是AllChannelsData。

For i = 0 To UBound(channelList) 
    'the first row in the array is the channels names 
    AllChannelsData(i, 0) = channelList(i) 
    Set RecChannel = Rec.FindChannel(channelList(i), RecDevice.Name) 
    For j = 0 To total_time 
     AllChannelsData(i, j + 1) = RecChannelData.GetRecData(RecChannel, 1, 0) 
    Next 
Next 

謝謝!

回答

0

請參考下面的代碼。

Sub Array_test() 
    Dim GetRecData(9) As String 
    Dim AllChannelsData(9, 2) As String 
    For i = 0 To 9 
     GetRecData(i) = i 
     For j = 0 To 9 
      AllChannelsData(j, 0) = j 
      AllChannelsData(j, 1) = GetRecData(j) 
     Next j 
    Next i 
End Sub 
0

更改此:

For j = 0 To total_time 
    AllChannelsData(i, j + 1) = RecChannelData.GetRecData(RecChannel, 1, 0) 
Next 

這個

For j = 0 To total_time 
    AllChannelsData(i, j + 1) = RecChannelData.GetRecData(RecChannel, 1, j) 
Next 

可能?

我假設.GetRecData(RecChannel, 1, 0)方法的第三個參數是索引,因爲像你描述的1D數組不需要3個參數。如果不是這樣,您可能需要擴展GetRecData方法的功能/做/返回/等等。

+0

感謝您的快速回復。第三個參數不是索引。 GetRecData方法需要三個參數來返回一維數組。 – peetman

0

這個 「基地」 代碼工作

Option Explicit 

Sub main() 
Dim arr1 As Variant 
Dim arr2() As Variant 
Dim total_time As Integer, i As Integer, j As Integer 

total_time = 4 

ReDim arr2(0 To 3, 0 To total_time) 
For i = 0 To 3 
    arr2(i, 0) = i 
    For j = 1 To total_time 
     arr2(i, j) = GetRecData(j + 1) 
    Next j 
Next i 

End Sub 

Function GetRecData(n As Integer) As Variant 
ReDim arr(0 To n - 1) As Variant 
Dim i As Integer 

For i = 1 To n 
    arr(i - 1) = i 
Next i 

GetRecData = arr 
End Function 

只是它適應您的需求