如何“ReDim Preserve”在Excel 2007 VBA中的2D数组,以便我可以向数组添加行而不是列? [英] How can I "ReDim Preserve" a 2D Array in Excel 2007 VBA so that I can add rows, not columns, to the array?
问题描述
帮助文档说明ReDim保留myArray n,m)允许我使m更大,但不是n。然而,我需要增加行数(n),同时保留我的数据,而不是列(m)!
例如,我可能有一个(5,20 )数组,我想扩展到(10,20),同时保留我的数据。
似乎如果有一些方法来转置我的数组,做一个ReDim Preserve扩展列的数量,然后重新转置我的数组,我可以完成我想要的。
这是正确的方法吗?如果是这样,我该怎么做?
有没有更好的方法来完成我想要的?
解决了我自己的问题这是我如何解决我的问题。我创建了一个临时数组,将myArray的内容复制到临时数组,调整了myArray的大小,然后将内容从temp数组复制到myArray。
code> tempArray = myArray
ReDim myArray(1 To(UBound(myArray())* 2),1 To m)
For i = 1 To n
对于j = 1对于m
myArray(i,j)= tempArray(i,j)
下一个j
下一个i
如果有人可以提出更有效的方法来做到这一点,我很乐意听到。
I'm working with a dynamic array in Excel VBA. The number of columns (m) is fixed, however, I do not know how many rows (n) will be required.
The help documents state that ReDim Preserve myArray(n, m) allows me to make m larger, but not n. However, I need to increase the number of rows (n) while preserving my data, not columns (m)!
For example, I may have a (5,20) array that I would like to expand to (10,20) while preserving my data.
It seems that if there were some way to transpose my array, do a ReDim Preserve to expand the number of "columns", then re-transpose my array, I could accomplish what I want.
Is this the correct way to do this? If so, how can I do that?
Is there a better way to accomplish what I want?
Solved my own question; here's how I got around my problem. I created a temporary array, copied the contents of myArray to the temporary Array, resized myArray, then copied the contents back from the temp array to myArray.
tempArray = myArray
ReDim myArray(1 To (UBound(myArray()) * 2), 1 To m)
For i = 1 To n
For j = 1 To m
myArray(i, j) = tempArray(i, j)
Next j
Next i
If anyone can suggest a more efficient way to do this, I'd love to hear it.
这篇关于如何“ReDim Preserve”在Excel 2007 VBA中的2D数组,以便我可以向数组添加行而不是列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!