2014-05-04 75 views
-1

我正嘗試用Double()數組創建鋸齒形數組。我只是試圖動態地將這個a()Double添加到d()中。當我嘗試從d中取出一個副本並將其放入x時,會出現錯誤。Excel VBA中的鋸齒形數組,類型不匹配錯誤

Sub Test3() 
Dim a() As Double, i As Integer 
ReDim a(1 To 10, 1 To 3) 
a(1, 2) = 3.5 

Dim d() As Variant 

For i = 1 To 3 
    ReDim Preserve d(1 To i) 
    d(i) = Array(a) 
Next i 


Dim x() As Double 
x = d(1) ' Error, Type Mismatch 

MsgBox (x(1, 2)) 

End Sub 
+0

它正在工作,當我ReDim x(1至10,1至3)。或者當我使用d(1)的大小時。有沒有辦法做到這一點,而不先調整x? – atomant

回答

1

試試這個。將數組(a)更改爲a。

Sub Test3() 
Dim a() As Double, i As Integer 
ReDim a(1 To 10, 1 To 3) 
a(1, 2) = 3.5 

Dim d() As Variant 

For i = 1 To 3 
    ReDim Preserve d(1 To i) 
    d(i) = a 
Next i 


Dim x() As Double 
x = d(1) ' Error, Type Mismatch 

MsgBox (x(1, 2)) 

End Sub