如何将Excel范围分配给2D数组? [英] How to assign an Excel Range to a 2D array?
问题描述
谢谢,
有一个简单的方法来修改一个使用数组的区域,将其写入同一个地方或其他地方。
此示例代码将使用数组将数据从一个区域复制到另一个区域:
子示例()
Dim testdata()
testdata = Range(A1:B13)
Range( D1:E13)= testdata'简单复制
范围(G1)= testdata'只复制1个单元格
范围(I1:K22)= testdata'尝试复制太多
End Sub
testdata
数组从 1 ,并将扩展到范围中指定的列和行数。在这种情况下, testdata(1,1)
是指从 A1 , testdata(1,2)
是指 B1 ,使用 testdata(13,1)
指向 A13 和 testdata(13,2)
引用 B13 。
设置与下一行中的数组相同的范围将数组复制到指定的位置。
- 如果该区域小于原始数组,它将仅复制足够的数组来填充该空间,因此
Range(D1)= testdata
只会在表单上放置一个单元格。 - 如果您指定较大的区域,则#N / A 将填充不在数组元素所涵盖的空间中的区域,因此
Range(A1:A3)= testdata
将使用数组填充A1和A2,但A3将具有#N / A
示例程序的结果:
注意:A1:B13是原始数据,随后的$ code> range(??)= testdata
Could you please say- how a Excel Range("G2:AA1000") can be assigned to a 2D array? If possible how to return back that 2D array to the same range after performing some operation on that 2D array?After assignment a Range to an 2D array,How each row will be identified from that 2D matrix?
Thanks,
There is an easy way to make changes to an area using an array, and write it out to the same place, or somewhere else.
This example code will copy data from one area to another, using an array:
Sub example()
Dim testdata()
testdata = Range("A1:B13")
Range("D1:E13") = testdata ' simple copy
Range("G1") = testdata ' copy only 1 cell
Range("I1:K22") = testdata 'try to copy too much
End Sub
The testdata
array starts from 1, and will extend to the number of columns and rows specified in the range. In this case, testdata(1,1)
refers to the data obtained from A1, testdata(1,2)
refers to B1, finishing up with testdata(13,1)
referring to A13, and testdata(13,2)
referring to B13.
Setting the range equal to the array in the next line copies the array into the specified location.
- If the area is smaller than the original array, it will copy only enough of the array to fill that space, so
Range("D1")=testdata
will only place one cell on the sheet. - If you specify a larger area, then #N/A will fill the area that is not in the space covered by array elements, so
Range("A1:A3")=testdata
will fill A1 and A2 with data from the array, but A3 will have #N/A
Result of example program:
Note: A1:B13 is the original data, which gets copied with the subsequent range(??)=testdata
这篇关于如何将Excel范围分配给2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!