2016-02-14 88 views
1

我有下面的代碼提取,它假設將數組的內容轉儲回工作表,但它似乎不工作,我卡住了..可以有人請幫忙,謝謝。轉儲Excel VBA數組到工作表不工作,

Dim aggressiveDriving(7000) As Variant 

For i = 3000 To 7000 
    'simple kinematic equation  
    aggressiveDriving(i) = Math.Sqr((((aggressiveDriving(i - 1)/3.6)^2) + (2 * aggressive_decel))) * 3.6 

Next 
'at this stage, when I watch the array, it is populated with the expected values in double. 
'now writing back to worksheet 

With ActiveWorkbook.Worksheets("Current_Driving") 
    'if I replace 'aggressiveDriving with '0' then the dumping works with 
    'filling 0s 
    .Range(.Cells(2, "F"), .Cells(7002, "F")).value = aggressiveDriving 
End With 

回答

2

的一維陣列的取向是目前7000「列」 1「行」,而不是7000「排'與1'欄'相比,工作表。使用工作表的TRANSPOSE function重定向它

With ActiveWorkbook.Worksheets("Current_Driving") 
    'if I replace 'aggressiveDriving with '0' then the dumping works with 
    'filling 0s 
    .Range(.Cells(2, "F"), .Cells(7002, "F")).value = _ 
     Application.Transpose(aggressiveDriving) 
    'I prefer this method of resizing (+1 because it is currently a zero-based index array) 
    '.Range("F2").Resize(UBound(aggressiveDriving) + 1, 1) = _ 
     Application.Transpose(aggressiveDriving) 

End With 

TRANSPOSE功能有限制;通常沿着舊的XLS工作表的尺寸線。

+0

謝謝Jeeped,工程就像一個魅力! – lukieleetronic

+0

好聽。只需注意[TRANSPOSE](https://support.office.com/en-us/article/TRANSPOSE-function-ED039415-ED8A-4A81-93E9-4B6DFAC76027)功能的限制;我認爲這是一個有符號的int - 1(例如32,767)。請參閱[數據文件中的Excel VBA中的Error13](http://stackoverflow.com/questions/29451126/error13-in-excel-vba-in-data-file)以獲取快速的home-spun函數** my_2D_Transpose ** with無限。 – Jeeped

+1

或者只是'Dim aggressiveDriving(1到7000,1)'而沒有轉置,因爲'cells'是一個二維數組,你還需要一個二維數組。邏輯否?此外,從1到3000的所有內容都將爲零,並被覆蓋。 –

0

循環

For i = 0 To 7000 
'simple kinematic equation  
aggressiveDriving(i) = Math.Sqr((((aggressiveDriving(i - 1)/3.6)^2) + (2 * aggressive_decel))) * 3.6 
Next 

開始從0到7000,然後在I = 0等式

aggressiveDriving(i) = Math.Sqr((((aggressiveDriving(i - 1)/3.6)^2) + (2 * aggressive_decel))) * 3.6 

,就會在索引-1

aggressiveDriving(i - 1) 
訪問數組

或者可能是陣列大小存在的問題。 數組大小是7000, 但試圖從F2寫:F7002 => 7001(大小)

+0

嘿,對不起循環假設從3000到7000開始,所以沒有問題..(我沒有問題填寫陣列..) – lukieleetronic

+0

數組大小實際上是7001不是嗎?因爲索引從0開始對吧?無論如何,我試圖減少範圍,但沒有幫助.. – lukieleetronic

+0

問題在於寫回到表:寫一維數組到2D ... –