我碰到這個問題,跌跌撞撞,同時擊中這條道路封鎖自己的所有新元素。我最後寫一段代碼,真正的快到一個新的大小的數組(第一個或最後一個維度)在處理這個ReDim Preserve
。也許它會幫助其他面臨同樣問題的人。
所以對於使用,可以說你有你的陣列最初設定爲MyArray(3,5)
,和你想的尺寸(第一呢!)放大,讓剛剛說MyArray(10,20)
。你會習慣做這樣的事情嗎?
ReDim Preserve MyArray(10,20) '<-- Returns Error
但不幸的是,由於您嘗試更改第一個維度的大小而返回錯誤。所以用我的功能,你只需要這樣做,而不是:
MyArray = ReDimPreserve(MyArray,10,20)
現在數組更大,數據被保留。您的多維數組的ReDim Preserve
已完成。 :)
最後但並非最不重要的,神奇的功能:ReDimPreserve()
'redim preserve both dimensions for a multidimension array *ONLY
Public Function ReDimPreserve(aArrayToPreserve,nNewFirstUBound,nNewLastUBound)
ReDimPreserve = False
'check if its in array first
If IsArray(aArrayToPreserve) Then
'create new array
ReDim aPreservedArray(nNewFirstUBound,nNewLastUBound)
'get old lBound/uBound
nOldFirstUBound = uBound(aArrayToPreserve,1)
nOldLastUBound = uBound(aArrayToPreserve,2)
'loop through first
For nFirst = lBound(aArrayToPreserve,1) to nNewFirstUBound
For nLast = lBound(aArrayToPreserve,2) to nNewLastUBound
'if its in range, then append to new array the same way
If nOldFirstUBound >= nFirst And nOldLastUBound >= nLast Then
aPreservedArray(nFirst,nLast) = aArrayToPreserve(nFirst,nLast)
End If
Next
Next
'return the array redimmed
If IsArray(aPreservedArray) Then ReDimPreserve = aPreservedArray
End If
End Function
我寫了這20分鐘就好,所以沒有保證。但是如果您想使用或擴展它,請隨時取用。我本來以爲有人會在這裏有這樣的代碼,顯然不是。所以,在這裏,你們會變成同類的減速機。
VB6不支持數組 – 2013-05-05 05:54:55
@EuroMicelli是它的陣列。您可以有一維變體陣列,變體可以包含數組。 – MarkJ 2013-09-10 11:45:15